problem in static function template specialization

M

Marco Jez

The following code reproduces a problem I'm having with the GCC compiler. It
compiles fine on MSVC, so I'm wondering whether it is legal C++ code or not.

template<typename C>
struct Provider
{
template<typename T>
static void func() {}
};

template<typename C, typename Pro>
struct Client
{
void create()
{
Pro::func<C>(); // compiler stops here
}
};

int main()
{
Client<int, Provider<double> > cli;
cli.create();
return 0;
}


The GNU compiler gives me a "parse error" at this line:

Pro::func<C>();

Note that if parameter T in Provider::func() was deducible automatically
(for example through a function argument), the code would compile. It seems
that the explicit template specialization above confuses the compiler.

Am I doing something wrong?

Cheers,
Marco
 
M

Malte Starostik

Marco said:
The following code reproduces a problem I'm having with the GCC compiler. It
compiles fine on MSVC, so I'm wondering whether it is legal C++ code or not.

template<typename C>
struct Provider
{
template<typename T>
static void func() {}
};

template<typename C, typename Pro>
struct Client
{
void create()
{
Pro::func<C>(); // compiler stops here
Pro::template func said:
}
};

int main()
{
Client<int, Provider<double> > cli;
cli.create();
return 0;
}


The GNU compiler gives me a "parse error" at this line:

Pro::func<C>();

Note that if parameter T in Provider::func() was deducible automatically
(for example through a function argument), the code would compile. It seems
that the explicit template specialization above confuses the compiler.

Am I doing something wrong?

See above. Similarly to the required use of typename with dependent
types (see the recent "trying to declare an iterator for a std::vector
of template pointers" thread), you have to explicitly tell the compiler
that Pro::foo is a template function here.

HTH,
Malte
 
V

Victor Bazarov

Marco said:
The following code reproduces a problem I'm having with the GCC
compiler. It compiles fine on MSVC, so I'm wondering whether it is
legal C++ code or not.
template<typename C>
struct Provider
{
template<typename T>
static void func() {}
};

template<typename C, typename Pro>
struct Client
{
void create()
{
Pro::func<C>(); // compiler stops here

I think you need

Pro::template func<C>();

here.

}
};

int main()
{
Client<int, Provider<double> > cli;
cli.create();
return 0;
}


The GNU compiler gives me a "parse error" at this line:

Pro::func<C>();

Note that if parameter T in Provider::func() was deducible
automatically (for example through a function argument), the code
would compile. It seems that the explicit template specialization
above confuses the compiler.
Am I doing something wrong?

Missing the 'template' keyword. See above.

V
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top