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 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
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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. ...
|
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 { };
...
|
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>
|
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...
|
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...
| |
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...
|
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)
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |