How to check if a function exists

I want to implement my own function std::make_unique when the function is part of the std namespace. I know that this helper function was added in C ++ 14, but I don’t have it in C ++ 11. So, I want to test it using some magic of the C ++ 11 template (could not find any options with macros) to check if a function exists inside the std , and if it does not define it itself. Is it possible? I don’t even know where to start.

+6
source share
5 answers

It is best to use the standard __cplusplus macro, which for C ++ 11 is 201103L . For C ++ 14, this will be a different value.

+6
source

You cannot add a new function to the ::std . You are allowed to add specializations to existing templates, and even then only for your own types.

+6
source

This is actually not anwser, but I want to indicate that the committee is considering (see n3694 ) the general question: How could a programmer determine if an implementation has a specific function (for example, std::make_unique ) or not?

Currently, the best we have is the __cplusplus macro (according to the Bathsheba post ), but as explained in n3694 , this does not produce the fine grain that programmers may need. (This question is an example of an OP).

+1
source

Introducing std::make_unique self undefined behavior, period. There is no safe way to do this. In addition, it is also impractical - the advantage that you get is small, and the cost of maintaining and understanding the code is high.

The more likely it is, your make_unique will not match what will be published exactly, so your code will have a weird version dependency.

A better plan is to define your own make_unique elsewhere in your own namespace. If C ++ 1y is active, you can use using std::make_unique import it instead of your own make_unique .

It is compatible with your code standards. It should also be clear to users of utility::make_unique that it is not guaranteed to be identical to C ++ 1y make_unique .

It also allows you to identify the std::make_unique detection problem until it is standardized, and then decide whether it matches the interface you completed.

+1
source
 #include <memory> // std::shared_ptr, std::unique_ptr // C++11 check doesn't work in Visual Studio 2013/2015 properly because they are not fully C++11 compliant. // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l #ifndef WIN32 // C++11 Check #if __cplusplus < 201103L #error This library needs at least a C++11 compliant compiler. #endif // >= C++11 #endif // WIN32 namespace std { // Note: despite not being even C++11 compliant, Visual Studio 2013 has their own implementation of std::make_unique. #ifndef WIN32 #if (__cplusplus >= 201103L && __cplusplus < 201402L) // Define std::make_unique for pre-C++14 template<typename T, typename... Args> inline unique_ptr<T> make_unique(Args&&... args) { return unique_ptr<T>(new T(forward<Args>(args)...)); } #endif // C++11 <= version < C++14 #endif // WIN32 } // namespace std 
0
source

All Articles