Referencing inherited template

I

Igor R.

Hello,

What is the correct, portable way to do the following:

template<template<class> class Derived, class T> class Base
{
Base(int i)
{}
};

template<typename T> class Derived : public Base<Derived, T>
{
public:
Derived(int i) : Base<Derived, T>(i) // doesn't compile with gcc 4.2
{}
};

Compiler error is:
error: type/value mismatch at argument 1 in template parameter list
for 'template<template<class> class Derived, class T> class Base'
expected a class template, got 'Derived<T>'


Thanks!
 
V

Victor Bazarov

Igor said:
Hello,

What is the correct, portable way to do the following:

template<template<class> class Derived, class T> class Base
{
Base(int i)
{}

This is a private c-tor. Make it public or protected to prevent access
error below.
};

template<typename T> class Derived : public Base<Derived, T>
{
public:
Derived(int i) : Base<Derived, T>(i) // doesn't compile with gcc 4.2

Try

Derived(int i) : Base< ::Derived, T>(i)

after making 'Base' accessible.
{}
};

Compiler error is:
error: type/value mismatch at argument 1 in template parameter list
for 'template<template<class> class Derived, class T> class Base'
expected a class template, got 'Derived<T>'

It's a bit tricky. "Derived" (naked) resolves to the type name (the
instantiation of the template), not the template name itself, inside
that template. The name resolution should take it "outside" and prevent
picking the instance name... At least it seems to work for Comeau.

V
 
I

Igor R.

This is a private c-tor.  Make it public or protected to prevent access error below.

Oh, sorry, in my real code it was public :).
 The name resolution should take it "outside" and prevent picking the instance name...

Great!
Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top