I have the following code:
namespace notstd{
// Only Declare this if std::make_unique exists
using std::make_unique;
// Only Declare this if std::make_unique DOES NOT exist
template<class T,class...Args> //
std::unique_ptr<T> make_unique(Args&&...args){
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
I would like the using declaration only declared if std::make_unique is defined (std::make_unique did not exist until C++14), otherwise, I would like to define my local version. I believe that this can be accomplished with SFINAE in C++11, but I cannot get it to work. Any advice?
I would not actually try to do this with SFINAE, especially since adding to the
stdnamespace is undefined behaviour except for certain limited scenarios like template specialisation - your approach seems to be less specialisation and more creation from scratch :-)Instead, you could just use the
__cplusplusmacro to conditionally compile code based on the supported level. This is set to201103for C++11 and201402for C++14, so should be easily usable.