View previous topic :: View next topic |
Author |
Message |
Akkara Bodhisattva


Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Wed Mar 30, 2016 9:59 am Post subject: C++ template syntax -- ARGH! |
|
|
I have a templated class, which contains a templated function. What's the syntax to define the function for a specialized version of the class?
I've tried various permutations of single and double templates, with parameters placed in various positions, and I can't seem to hit upon the correct syntax for some reason. This latest incarnation, compiled with g++-4.7.2, gives: Quote: | error: function template partial specialization ‘doit<b>’ is not allowed
error: new declaration ‘void Something<32>::doit(int*, unsigned int, unsigned int)’
error: ambiguates old declaration ‘void Something<iA>::doit(int*, unsigned int, unsigned int) [with int iB = iB; int iA = 32]’ |
Ambiguates. That's a new one for me
Code: | #include <stdio.h>
template <int iA>
struct Something {
template <int iB>
void doit(int *, unsigned, unsigned);
};
template <>
template <int b>
void Something<32>::doit<b>(int *out, unsigned stride, unsigned n)
{
printf("In Something<%d>::doit<%d>\n", 32, b);
} |
--------------------
What if I try pre-instantiating the class? Code: | template <int iA>
struct Something {
template <int iB>
void doit(int *, unsigned, unsigned);
};
template <>
struct Something<32>;
template <>
template <int b>
void Something<32>::doit<b>(int *out, unsigned stride, unsigned n)
{
printf("In Something<%d>::doit<%d>\n", 32, b);
} |
Nope.: Quote: | error: too many template-parameter-lists |
Get rid of the template <> in front of the definition? Still nope: Quote: | error: expected initializer before ‘<’ token |
I'm sure there's some rhyme and reason to all this, but I can't seem to figure it out right now.
Thanks! _________________ Many think that Dilbert is a comic. Unfortunately it is a documentary. |
|
Back to top |
|
 |
radio_flyer Guru


Joined: 04 Nov 2004 Posts: 318 Location: Northern California
|
Posted: Wed Mar 30, 2016 5:43 pm Post subject: |
|
|
Function template partial specialization is not allowed by the standard..it has ambiguity issues with overloading. I believe MSVC allows it, but really it shouldn't. See http://www.gotw.ca/publications/mill17.htm. |
|
Back to top |
|
 |
SiberianSniper Guru


Joined: 06 Apr 2006 Posts: 381 Location: Dayton, OH, USA
|
Posted: Wed Mar 30, 2016 5:49 pm Post subject: |
|
|
Code: | #include <stdio.h>
template <int iA>
struct Something {
template <int iB>
void doit(int *, unsigned, unsigned) {};
};
template <>
template <>
void Something<32>::doit<16>(int *out, unsigned stride, unsigned n)
{
printf("In Something<%d>::doit<%d>\n", 32, 16);
}
int main()
{
Something<32> s;
s.doit<16>(0, 0, 0);
}
|
That compiles, though it doesn't give you the iA and iB inside the specialization.
Unfortunately, playing around with g++ and clang++ both tell me that you can't specialize for a only the class. I'm not sure if that's true or not, since I haven't looked at the C++ spec yet. |
|
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
|
|