Search code examples
c++bindwrapper

How can I wrap a function (std::bind) into a namespaces?


I want to wrap the bind class template into an separate namespace:

namespace my_space {
template<typename... R> using bind = std::bind<R...>;
}

and get an error:

error: 'bind<R ...>' in namespace 'std' does not name a type.

How am i able to do so? A small example can be found here.


Solution

  • Why your code does not work

    Your code doesn't compile because std::bind is a function, not a type. You can declare aliases using using only for types.

    While g++ diagnostic is not exactly the best, Clang++ would have given you the following error:

    error: expected a type

    which is much clearer*.

    What you can do

    Thankfully you can just import the std::bind name using:

    namespace my_space {
        using std::bind;
    }
    

    Live demo

    This is specifically defined in:

    §7.3.3/1 The using declaration [namespace.alias]

    A using-declaration introduces a name into the declarative region in which the using-declaration appears.

    * Personal opinion.