Template class partial specialisation problem

From: Lionel B (google_at_lionelb.com)
Date: 11/03/04


Date: 3 Nov 2004 06:44:28 -0800

Greetings.

The following code compiles ok and does what I'd expect it to do:

[apologies for the lack of indentation... Google Groups 2 Beta bug
reported]

---------- START CODE ----------

// test.cpp

#include <iostream>

template <class T> struct A
{
A();
};

A<int>::A()
{
std::cout << "Template parameter is \"int\"" << '\n';
}

A<long>::A()
{
std::cout << "Template parameter is \"long\"" << '\n';
}

int main()
{
A<int> a1;
A<long> a2;

return 0;
}

---------- END CODE ----------

The program outputs:

Template parameter is "int"
Template parameter is "long"

So far so good... now I would like to do the same, but there is a
second template parameter and I only want to specialise on the first:

---------- START CODE ----------

// test.cpp

#include <iostream>

template <class T, class R> struct A
{
A();
};

template <class R> A<int, R>::A() // line 10
{
std::cout << "First template parameter is \"int\"" << '\n';
}

template <class R> A<long, R>::A() // line 15
{
std::cout << "First template parameter is \"long\"" << '\n';
}

int main()
{
A<int, int> a1;
A<long, int> a2;

return 0;
}

---------- END CODE ----------

My compiler (gcc 3.3.3 cygwin special) throws this out with:

g++ test.cpp -o test.exe
test.cpp:10: error: parse error before `)' token
test.cpp:15: error: parse error before `)' token

I can't figure out quite what the syntax should be to specialise on the
first template parameter only. It would seem that A<int, R>::A() is
still a template function, but my compiler doesn't like the "obvious"
definition:
template <class R> A<int, R>::A()

Any help appreciated,

-- 
Lionel B


Relevant Pages

  • Error, if compiled with -O3 (gettext)
    ... int main ... t1.cxx:11: error: parse error before `,' token ... And now the old compiler 2.95: ... gcc version 2.95.4 20011002 (Debian prerelease) ...
    (comp.lang.cpp)
  • Re: Template function question
    ... with this code (why the compiler accepts the call to DoSomething2 but ... template parameter 'T' is ambiguous ... // or 'const int &' ... the compiler deduces T to be 'const int&', ...
    (microsoft.public.vc.language)
  • Re: ofstream and rdstate()
    ... >>int main ... Compiler: Default compiler ... Untitled1.cpp:20: no matching function for call to ... Untitled1.cpp:22: parse error at end of saved function text ...
    (alt.comp.lang.learn.c-cpp)
  • Re: friend function of template
    ... >> template parameter". ... >> This can be compiled under the online compiler Comeau. ... > template <int L, int M, int T> ...
    (microsoft.public.vc.language)
  • Constructor Syntax Problem
    ... Fee fee = 1; ... He claims this should all work but my compiler complains as follows: ... ex19.cpp:12: parse error at null character ... ex19.cpp: In function 'int main': ...
    (comp.lang.cpp)