Dynamic use of namespace based on template parameter

I don’t know if this is possible at all, but I would like to achieve this: in the template class, I would like to use the namespace of the template parameter.

eg.

template<class P>
class Foo
{
    public:
        Foo();
        virtual ~Foo();

        void doSomething(P&);
        void doSomethingElse();

    protected:
        // There I'm hardcoding "namespace1" but that what I'd like to 
        // be possibly dynamic 
        // (I'm assuming template parameter P = namespace1::Type)
        void method1(namespace1::Type1&);
        ...
        void methodN(namespace1::TypeN&);
}

// Again, supposing P == namespace1::Type then I want to be using namespace1 
// everywhere in the implementation...
using namespace namespace1;

template<class P>
void Foo<P>::doSomething(P& parameter)
{
    ...
    Type1 type1 = P.getType1(); // There namespace1::Type1 is returned !!
    method1(type1);
    ...
}

template<class P>
void Foo<P>::doSomethingElse()
{
    ...
    TypeN typen; // There I want to instanciate a namespace1::TypeN !!
    ...
}

...

Of course, I do not want to specialize the template and provide a dedicated implementation for each possible value P, and I would also like to avoid passing all types, such as Type1and TypeN, as template parameters, since I have a lot of them.

Is it possible?

The project is based on C ++ 3, any solution for speeding up is welcome.

Update

Being a template parameter P, like any parameter TypeN, this may be the right approach:

template<typename NAMESPACE>
class Foo
{
    typedef typename NAMESPACE::Parameter MyParameter; 
    typedef typename NAMESPACE::Type1 MyType1; 
    typedef typename NAMESPACE::Type1 MyTypeN; 
    ...
}
+5
source share
3

.

, , :

template <typename T> struct Trait { typedef typename T::Secondary Secondary; };

template <typename X>
struct Foo {
    typedef typename Trait<X>::Secondary Secondary;

    void foo(Secondary const& s);
};

, , , ; , (typedef ...) .

+5

, , .

//my_class.inl
template<TYPE_ARG>
struct my_class_t<TYPE_ARG>
{
   foo()
   {
      NS_ARG::bar();
   }
}

//my.h
template<class T>
struct my_class_t{};

#define TYPE_ARG T1
#define NS_ARG std
#include "my_class.inl"

#define TYPE_ARG T2
#define NS_ARG boost
#include "my_class.inl"

#undef TYPE_ARG
#undef NS_ARG

, . ?: )

0

, , , ADL, :

#include <iostream>
#include <utility>

namespace Foo_ns
{
    struct T1
    {
        static void m() { std::cout << "Foo_ns::T1" << '\n'; }
    };
    struct Foo {};

    // List of all the types you need from this namespace
    struct Types
    {
        typedef Foo_ns::T1 T1;        
    };

    // dummy function needed for ADL        
    Types types(...);
}

namespace Bar_ns
{
    struct T1
    {
        static void m() { std::cout << "Bar_ns::T1" << '\n'; }
    };
    struct Bar {};

    struct Types
    {
        typedef Bar_ns::T1 T1;
    };

    Types types(...);
}

template <typename T>
void callMOnT1(const T &arg)
{
    typedef decltype(types(std::declval<T>())) Types; // ADL kicks in
    //typedef typename std::result_of<types(T)>::type Types;
    Types::T1::m();
}

int main()
{
    callMOnT1((Bar_ns::Bar())); // Bar_ns::T1
    callMOnT1((Foo_ns::Foo())); // Foo_ns::T1
}

Unfortunately, this solution uses some C ++ 11 features (namely decltype, declval). For some reason, I was unable to get the code to work with result_ofwhich is present in Boost (maybe someone can explain why the code with result_ofdoes not compile?).

0
source

All Articles