Gasim
Gasim

Reputation: 7961

how to check if function exists

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

Upvotes: 7

Views: 2927

Answers (5)

Grayson Lang
Grayson Lang

Reputation: 164

#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

Upvotes: 0

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275210

Introducing std::make_unique yourself is undefined behavior, period. There is no safe way to do it. On top of that, it is also inadvisable -- the advantage you gain is small, and the maintenance and code understandability costs are high.

What more, the odds are your make_unique will not match whatever gets published exactly, so your code will have a strange version dependency.

A better plan is to define your own make_unique somewhere else in your own utility namespace. If C++1y is active, you could using std::make_unique import it instead of your own make_unique.

This keeps your code standards compliant. It should also be clear to users of utility::make_unique that it isn't guaranteed to be identical to C++1y's make_unique.

This also lets you punt the problem of detecting std::make_unique until after it is standardized, and then deciding if it matches the interface you ended up implementing.

Upvotes: 1

Cassio Neri
Cassio Neri

Reputation: 20503

This is not really an anwser but I want to point out that the committee is considering (see n3694) the generic question: How a programmer could determine whether an implementation has a particular feature (e.g. std::make_unique) or not?

Currently, the best we have is the macro __cplusplus (as per Bathsheba's post) but as explained in n3694 this doesn't give the fine grain that programmers might need. (The OP's question is an example of this need.)

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234635

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

Upvotes: 6

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

Upvotes: 6

Related Questions