Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic namespace usage based on a template parameter

I don't know if that is feasable at all, but this is what I'd like to achieve : in a templated class I would like to be using 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's 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 don't want to specialize the template and provide a dedicated implementation for every possible P value as well as I'd like to avoid passing all the types like Type1 and TypeN as template parameters since I potentially have lots of them.

Is that possible ?

The project is C++3 based, any boost solution is welcome.

Update

Being the template parameter P itself exactly like any TypeN parameter, this could be the right approach :

template<typename NAMESPACE>
class Foo
{
    typedef typename NAMESPACE::Parameter MyParameter; 
    typedef typename NAMESPACE::Type1 MyType1; 
    typedef typename NAMESPACE::Type1 MyTypeN; 
    ...
}
like image 400
codeJack Avatar asked Feb 22 '13 14:02

codeJack


People also ask

Which is correct example of template parameters?

For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.

Can we pass Nontype parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.

Can a template parameter be a function?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

How do you use template arguments in C++?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)


1 Answers

Yes and No.

Yes it is possible to deduce secondary types from a primary one, generally using a trait system:

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);
};

No, you cannot deduce a namespace, and thus cannot use it; but note how by using a local alias (typedef ...) within the class there is no need to.

like image 78
Matthieu M. Avatar answered Sep 27 '22 18:09

Matthieu M.