c++templatesdependent-name

refer to a non-dependent name without specifying template parameter


Consider the following:

template<typename T> struct Foo {
     typedef void                  NonDependent;
     typedef typename T::Something Dependent;
}

I would like to refer to NonDependent without specifying any template parameter, as in Foo::NonDependent.

I know I can always use a dummy parameter:

Foo<WhateverSuits>::NonDependent bla;

But that is ugly, and since NonDependent is invariant with respect to T, I would like to refer to it without relying on the dummy. Is it possible?

Thanks


Solution

  • You can not refer to NonDependent without specifying template parameter because it may vary or completely absent depending on template parameter. For example:

    template<> struct Foo<int>
    {
       typedef float NonDependent;
    };
    template<> struct Foo<std::string>
    {
       typedef typename std::string::value_type Dependent;
    };
    

    You may need to move NonDependent declaration into base (non-template) struct and refer to it instead:

    struct FooBase{ typedef void NonDependent; };
    
    template<typename T> struct Foo: public FooBase
    {
        typedef typename T::Something Dependent;
    };
    template<> struct Foo<int>: public FooBase
    {
       typedef float NonDependent;
    };
    
    FooBase::NonDependent bla;