 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Victor Bazarov Guest
|
Posted: Fri Jun 27, 2003 7:54 pm Post subject: Re: Specialization of Class Templates |
|
|
"foo" <maisonave (AT) axter (DOT) com> wrote...
| Quote: | I was trying to test out the GNU (3.x) compiler with specialization
and partial specialization.
I'm having a problem with the syntax, or with the compiler. I'm not
sure which.
The following is the code that I'm getting compile errors with. I
have comments next to the line of code that is giving me compile
errors.
Could some one please tell me what is the correct syntax?
template <int a, int b
struct f {
void operator()() {
std::cout << a << ' ' << b << std::endl;
}
void SomeFunc1()
{
std::cout << a << " Reg1 " << b << std::endl;
}
void SomeFunc2();
template
void SomeFunc3(const T&);
};
template
void f<3,2>::SomeFunc1()
{
//`a' undeclared (first use this function)
//I get the above compile error for the following line
std::cout << a << " <3,2> " << b << std::endl;
|
Well, it _is_ undeclared. The template argument names only
have meaning in the _declarative_scope_ of that template
declaration. This function body is a _different_ declarative
region, which doesn't have any 'a' or 'b' in it.
| Quote: | }
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
|
Here you have 'a' and 'b' because you chose to name template
arguments that way. If you named template arguments 'x' and 'y',
what would you expect to use in the output statement?
| Quote: | }
//template parameters specified in specialization
//The following code produces the above error
template
void f
{
std::cout << a << " Partial specialization " << b << std::endl;
|
The same problem. 'a' is a template argument. 'b' is nothing.
You replaced 'b' with 3. There is no more 'b'.
| Quote: | }
template
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
|
Do you need any comment from me here or do you already see
the pattern?
| Quote: | }
//enclosing class templates are not explicitly specialized
//The following code produces the above error
|
The Standard prohibits specialisation of template members
of unspecialised template. You have to specialise the class
itself first before you can specialise a member template.
| Quote: | template
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
}
|
HTH
Victor
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Fri Jun 27, 2003 8:38 pm Post subject: Re: Specialization of Class Templates |
|
|
On 27 Jun 2003 12:33:51 -0700, [email]maisonave (AT) axter (DOT) com[/email] (foo) wrote:
| Quote: | I was trying to test out the GNU (3.x) compiler with specialization
and partial specialization.
I'm having a problem with the syntax, or with the compiler. I'm not
sure which.
The following is the code that I'm getting compile errors with. I
have comments next to the line of code that is giving me compile
errors.
Could some one please tell me what is the correct syntax?
template <int a, int b
struct f {
void operator()() {
std::cout << a << ' ' << b << std::endl;
}
void SomeFunc1()
{
std::cout << a << " Reg1 " << b << std::endl;
}
void SomeFunc2();
template
void SomeFunc3(const T&);
};
template
void f<3,2>::SomeFunc1()
{
//`a' undeclared (first use this function)
//I get the above compile error for the following line
std::cout << a << " <3,2> " << b << std::endl;
|
There is nothing named a or b in scope here. The names of template
parameters of a class aren't injected as members of that class. The
correct syntax is:
std::cout << 3 << " <3,2> " << 2 << std::endl;
| Quote: | }
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
}
//template parameters specified in specialization
//The following code produces the above error
template
void f
{
std::cout << a << " Partial specialization " << b << std::endl;
|
You can't partially specialize a member function without partially
specializing the whole class. In addition, there is no b in scope even
if it were legal.
The correct approach is to partially specialize f:
template
struct f<a, 3>
{
void SomeFunc2();
};
Now you can do:
template <class a>
void f<a, 3>::SomeFunc2()
{
}
| Quote: | }
template <int a, int b
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
}
//enclosing class templates are not explicitly specialized
//The following code produces the above error
template
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
}
|
You can't specialize a template member without fully specializing the
enclosing class.
All of these are standard rules, not G++ specific ones.
Tom
|
|
| Back to top |
|
 |
foo Guest
|
Posted: Sat Jun 28, 2003 5:32 am Post subject: Re: Specialization of Class Templates |
|
|
[email]tom_usenet (AT) hotmail (DOT) com[/email] (tom_usenet) wrote in message news:<3efca90d.2116031 (AT) news (DOT) easynet.co.uk>...
| Quote: | On 27 Jun 2003 12:33:51 -0700, [email]maisonave (AT) axter (DOT) com[/email] (foo) wrote:
I was trying to test out the GNU (3.x) compiler with specialization
and partial specialization.
I'm having a problem with the syntax, or with the compiler. I'm not
sure which.
The following is the code that I'm getting compile errors with. I
have comments next to the line of code that is giving me compile
errors.
Could some one please tell me what is the correct syntax?
template <int a, int b
struct f {
void operator()() {
std::cout << a << ' ' << b << std::endl;
}
void SomeFunc1()
{
std::cout << a << " Reg1 " << b << std::endl;
}
void SomeFunc2();
template
void SomeFunc3(const T&);
};
template
void f<3,2>::SomeFunc1()
{
//`a' undeclared (first use this function)
//I get the above compile error for the following line
std::cout << a << " <3,2> " << b << std::endl;
There is nothing named a or b in scope here. The names of template
parameters of a class aren't injected as members of that class. The
correct syntax is:
std::cout << 3 << " <3,2> " << 2 << std::endl;
}
template
void f
{
std::cout << a << " Reg3 " << b << std::endl;
}
//template parameters specified in specialization
//The following code produces the above error
template
void f
{
std::cout << a << " Partial specialization " << b << std::endl;
You can't partially specialize a member function without partially
specializing the whole class.
|
Ahh! That is the information I was looking for.
I was under the incorrect impression that you can do a partial
specialization on a function, with out having to declare a partial
specialized class for it.
Know I understand, and the other errors make sense to me know.
Thanks
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|