Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: Template overloading on return type. (http://www.velocityreviews.com/forums/t959527-re-template-overloading-on-return-type.html)

James Kanze 04-08-2013 09:34 AM

Re: Template overloading on return type.
 
On Friday, 5 April 2013 10:27:00 UTC+1, Glen Stark wrote:

> Let's say I have a situation like this:


> class Node
> {
> ...
> template <typename T> getValue()
> { /** generic implementation code here */}



> }


> Which allows me to call


> int bla = node.getValue<int>();


> Now this works for everything except bool, for which I have to do
> something different. How do I go about implenting that? I.e. how do
> instruct the compiler to instantiate a different set of instructions in
> the case that T is bool?


An explicit specialization should do the trick:

template<>
bool Node::getValue<bool>()
{
// ...
}

--
James

Luca Risolia 04-08-2013 07:23 PM

Re: Template overloading on return type.
 
On 08/04/2013 21:20, Andy Champ wrote:
>> template<>
>> bool Node::getValue<bool>()
>> {
>> // ...
>> }


> That's odd. That's pretty well what I said 3 days ago - did my post get
> lost?


No.


Stuart 04-08-2013 07:45 PM

Re: Template overloading on return type.
 
On 04/08/2013, Andy Champ wrote:
> On 08/04/2013 10:34, James Kanze wrote:
>> An explicit specialization should do the trick:
>>
>> template<>
>> bool Node::getValue<bool>()
>> {
>> // ...
>> }
>>
>> -- James

>
> That's odd. That's pretty well what I said 3 days ago - did my post get
> lost?


Minor nitpick: James mentions the term "explicit specialization" so that
OP knows how it is called. And his version is compilable ;-)

Dunno, I would have expected that he would answer to your posting and
just add how this thing is called.

Regards,
Stuart

Öö Tiib 04-09-2013 01:22 PM

Re: Template overloading on return type.
 
On Tuesday, 9 April 2013 14:15:01 UTC+3, Andy Champ wrote:
> On 08/04/2013 20:45, Stuart wrote:
>
> > And his version is compilable ;-)

>
> OP's class:
> class Node
> {
> ...
> template <typename T> getValue()
> { /** generic implementation code here */}
>
> }
>
> My modified one:
>
> class Node
> {
> public:
> template <typename T> T getValue()
> { return 0;}
>
> template <> bool getValue()
> { return true; }
> };
>
> Compiles all right over here...


Interesting. Feels that it should not compile. Like I understand: An
explicit specialization shall be declared in a namespace enclosing
the specialized template (§ 14.7.3/2) and the definition of a class
shall precede the declaration of an explicit specialization for a
member template of the class (§ 14.7.3/3). My attempts confirm it
like that:

class Node
{
public:
template <typename T>
T getValue() { return 0;}

template <>
bool getValue() // error: explicit specialization in non-namespace
// scope 'class Node' template <> bool getValue()
{ return true; }
};

int main()
{
Node n;
bool b = n.getValue<bool>();
}

I suspect that you have somehow "extended" compiler.

James Kanze 04-09-2013 06:20 PM

Re: Template overloading on return type.
 
On Monday, 8 April 2013 20:20:22 UTC+1, Andy Champ wrote:
> On 08/04/2013 10:34, James Kanze wrote:
> > An explicit specialization should do the trick:


> > template<>
> > bool Node::getValue<bool>()
> > {
> > // ...
> > }


> That's odd. That's pretty well what I said 3 days ago - did my post get
> lost?


You had the syntax wrong. I think that was all. (But I don't
always see posts as soon as I should, and mine don't always
appear immediately either.)

--
James

Stuart 04-11-2013 02:53 PM

Re: Template overloading on return type.
 
On 04/09/13, Andy Champ wrote:
> On 08/04/2013 20:45, Stuart wrote:
>> And his version is compilable ;-)

>
> OP's class:
> class Node
> {
> ...
> template <typename T> getValue()
> { /** generic implementation code here */}
>
>
> }
>
> My modified one:
>
> class Node
> {
> public:
> template <typename T> T getValue()
> { return 0;}
> template <> bool getValue()
> { return true; }
> };
>
> Compiles all right over here...
>
> Andy


I meant your original posting, where you simply put "..." inside the
method body. James did the same, but he put it into a comment " // ...".
Of course, both of your versions will yield a compiler error because
neither had a return statement, hence the smiley.

Regards,
Stuart


All times are GMT. The time now is 06:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.