Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > g++ vs msvc++

Reply
Thread Tools

g++ vs msvc++

 
 
rbfish@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
Anyone can tell me g++ can NOT compile the simple program with error:
test.cpp:12: error: invalid explicit specialization before '>' token
test.cpp:12: error: explicit specialization in non-namespace scope
`class C'
test.cpp:14: error: invalid member function declaration

But MSVC++ can.

Thanks
rbfish

#include "stdio.h"

class C
{
public:
template<int N>
void f()
{
printf("f<N=%d>()\n", N);
}

template<>
void f<1>()
{
printf("f<1>()\n");
}
};

int main()
{
C c;
c.f<1>();
c.f<2>();

return 0;
}

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      01-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> #include "stdio.h"
>
> class C
> {
> public:
> template<int N>
> void f()
> {
> printf("f<N=%d>()\n", N);
> }
>
> template<>
> void f<1>()
> {
> printf("f<1>()\n");
> }
> };
>
> int main()
> {
> C c;
> c.f<1>();
> c.f<2>();
>
> return 0;
> }


> Anyone can tell me g++ can NOT compile the simple program with error:
> test.cpp:12: error: invalid explicit specialization before '>' token
> test.cpp:12: error: explicit specialization in non-namespace scope
> `class C'
> test.cpp:14: error: invalid member function declaration
>
> But MSVC++ can.


Because gcc is correct and Visual C++ is wrong.

The error says it all. You can't have an explicit specialization in a
class declaration. The explicit specialization must occur at namespace
scope:

template<>
void C::f<1>()
{
printf("f<1>()\n");
}

Best regards,

Tom

 
Reply With Quote
 
 
 
 
eiji
Guest
Posts: n/a
 
      01-31-2006
> c.f<1>();
> c.f<2>();


You try to use the template -typename as argument!
This should not be possible!

C c;
c.f<int>(1);
c.f<double>(2);
print 1 as 1 and 2 as 2.0

 
Reply With Quote
 
eiji
Guest
Posts: n/a
 
      01-31-2006
Okay, I was wrong. I should grap a templates-book on
expression-templates /metaprogramming/etc.
Sorry

 
Reply With Quote
 
rbfish@hotmail.com
Guest
Posts: n/a
 
      01-31-2006
Thank you very much.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments