Search code examples
c++generic-programmingc++20c++-concepts

Doesn't using directives work with c++20 concepts?


Does using directives not work with concepts? why?

The example below doesn't work and I get a compiler error saying that it expected a type.

#include <concepts>

namespace A::X {

  struct BaseA {};

  template < typename AType >
    concept DerivedFromA = std::derived_from < AType, BaseA >;

}

namespace A {

  using DerivedFromA = X::DerivedFromA;

}

I want to be able to access concept DerivedFromA from namespace A as DerivedFromA rather than X::DerivedFromA


Solution

  • Does using directives not work with concepts? why?

    Because the way you're employing using declares a type alias. And concepts are not type templates.

    What you intend to do is to make a name from one namespace available in a different one. That looks like this:

    namespace A
    {
      using X::DerivedFromA;
    }
    

    This works on all kinds of names.