Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
C++ template syntax -- ARGH!
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Wed Mar 30, 2016 9:59 am    Post subject: C++ template syntax -- ARGH! Reply with quote

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
View user's profile Send private message
radio_flyer
Guru
Guru


Joined: 04 Nov 2004
Posts: 318
Location: Northern California

PostPosted: Wed Mar 30, 2016 5:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
SiberianSniper
Guru
Guru


Joined: 06 Apr 2006
Posts: 381
Location: Dayton, OH, USA

PostPosted: Wed Mar 30, 2016 5:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
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