473,508 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in static function template specialization

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
Jul 23 '05 #1
3 1762
Marco Jez schrieb:
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<C>(); // correct }
};

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
Jul 23 '05 #2
Marco Jez wrote:
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
Jul 23 '05 #3
> I think you need

Pro::template func<C>();

here.


Thanks Victor and Malte. I must admit I've never seen this syntax before...
:-)

Cheers,
Marco
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6448
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
13
2797
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
5
6565
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
290
by: Imre | last post by:
Hi I'd like to know what the problem is with the following code. I've tried compiling it with two compilers (VC++ 8.0, and Comeau online compiler), and both failed to compile it, saying that...
1
1601
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum...
1
2125
by: Bari | last post by:
Hi, When i do the following specializition which commented in the source then including the header in two cpp files gives me an error: test3.obj : error LNK2005: "class A<int* __cdecl...
5
1629
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
7
1999
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
13
6554
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.