L. Bruce
L. Bruce

Reputation: 300

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

Upvotes: 2

Views: 64

Answers (2)

Petok Lorand
Petok Lorand

Reputation: 973

As previously stated this can not be done. Mainly because Foo is not a type available at run-time, rather a template available at compile time, so things like Foo::NonDependent are invalid.

In orther to have something available at run time you have to instantiate the template, providing the minimum number of arguments in order to produce a class, of which subtypes can be referred to at run time.

Upvotes: 0

user7860670
user7860670

Reputation: 37520

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;

Upvotes: 6

Related Questions