473,890 Members | 1,380 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<typena me C>
struct Provider
{
template<typena me T>
static void func() {}
};

template<typena me 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 1785
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<typena me C>
struct Provider
{
template<typena me T>
static void func() {}
};

template<typena me 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<typena me C>
struct Provider
{
template<typena me T>
static void func() {}
};

template<typena me 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
6501
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. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
13
2833
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 { }; template <typename Base> class Tpl : protected Base {
5
6596
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 there's no B::G() function. It seems that the primary version of the CallFHelper template class is selected for both A and B, though I think that the specialization should be selected for B (after all, IntToVoid<sizeof(Dummy(&T::F))>::type _is_...
1
1624
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 DirectionType{ dtX,dtY,dtXD,dtYD }; template<typename T> class Point{
1
2141
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 makeA<int>(void)" (??$makeA@H@@YAPAV?$A@H@@XZ) already defined in testSTL.obj Can you please tell me how i can specialize the template function and still use the header in multiple cpp files.
5
1658
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
2036
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 default value std::vector<T>). To handle the different ways that the values are randomized, I created a traits class. The problem that I have, is that the function randomize() within the traits class takes different parameters depending on the type...
13
6616
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9825
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6052
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
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 we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3282
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.