Re: Template overloading on return type.

Discussion in 'C++' started by James Kanze, Apr 8, 2013.

  1. James Kanze

    James Kanze Guest

    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
    James Kanze, Apr 8, 2013
    #1
    1. Advertising

  2. James Kanze

    Luca Risolia Guest

    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.
    Luca Risolia, Apr 8, 2013
    #2
    1. Advertising

  3. James Kanze

    Stuart Guest

    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
    Stuart, Apr 8, 2013
    #3
  4. James Kanze

    Öö Tiib Guest

    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.
    Öö Tiib, Apr 9, 2013
    #4
  5. James Kanze

    James Kanze Guest

    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
    James Kanze, Apr 9, 2013
    #5
  6. James Kanze

    Stuart Guest

    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
    Stuart, Apr 11, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josephine Schafer

    Re: Overloading return type. I'm lost.

    Josephine Schafer, Jul 18, 2003, in forum: C++
    Replies:
    1
    Views:
    306
    John Harrison
    Jul 18, 2003
  2. Val
    Replies:
    2
    Views:
    355
    baojian
    Jul 20, 2003
  3. dropy
    Replies:
    2
    Views:
    325
    Jonathan Mcdougall
    Jul 28, 2003
  4. Damon
    Replies:
    2
    Views:
    11,238
    Damon
    Dec 15, 2003
  5. Ann Huxtable
    Replies:
    9
    Views:
    726
    benben
    Jul 17, 2005
Loading...

Share This Page