a hard problem about template specification

Discussion in 'C++' started by wangjk@ihep.ac.cn, Jun 20, 2008.

  1. Guest

    Hi,
    I know there are many experienced C++ experts be here, i have a
    puzzle :

    (1) template <typename L, typename R, bool rL = false, bool rR =
    flase > class A{.......};

    (2) template <typename L, typename R> class
    A<L,R,true,true>{......};

    and there is a partial specification operator :
    (3) template < >A<L,R>::eek:perator()(int){......};


    int main(){
    ......
    return (new A<int,int,true,true>())->(5);
    ...

    }


    in the main function, in the return sentence ......->(5);, we called
    the operator(),
    I think it should call class (2)'s or it's base class's operator (),
    but the result is that it called the operator in (3).

    Who can tell me some? thanks!
    , Jun 20, 2008
    #1
    1. Advertising

  2. Eric Pruneau Guest

    <> a écrit dans le message de news:
    ...
    > Hi,
    > I know there are many experienced C++ experts be here, i have a
    > puzzle :
    >
    > (1) template <typename L, typename R, bool rL = false, bool rR =
    > flase > class A{.......};
    >
    > (2) template <typename L, typename R> class
    > A<L,R,true,true>{......};
    >
    > and there is a partial specification operator :
    > (3) template < >A<L,R>::eek:perator()(int){......};
    >
    >
    > int main(){
    > ......
    > return (new A<int,int,true,true>())->(5);
    > ...
    >
    > }
    >
    >
    > in the main function, in the return sentence ......->(5);, we called
    > the operator(),
    > I think it should call class (2)'s or it's base class's operator (),
    > but the result is that it called the operator in (3).
    >
    > Who can tell me some? thanks!
    >


    What is sure is that
    new A<int,int,true,true>()
    calls the constructor of (2).

    Well (3) is not very clear to me... you should paste a more detailed code.
    But what I understand is that the specialization of A (case 2) has an
    operator() taking an int (looks like there is no return type!).

    so after creating A you call operator() with 5 as the argument, so it calls
    (3)


    here is what I think you wanna do

    template<typename T, typename L, bool b1=false, bool b2=false>
    struct A
    {
    A() {cout<<"1\n";}
    };

    template <typename T, typename L>
    struct A<T, L,true,true>
    {
    A(){cout<<"2\n";}
    int operator()(int i);
    };

    template<typename T, typename L>
    A<T,L,true,true>::eek:perator()(int i)
    {
    cout<< i<<endl;
    }

    int main()
    {
    return (new A<s,s,true,true>)->operator()(5);
    }

    this print
    2
    5


    ---------------

    Eric Pruneau
    Eric Pruneau, Jun 20, 2008
    #2
    1. Advertising

  3. Road.Tang Guest

    On Jun 20, 9:55 am, wrote:
    > Hi,
    > I know there are many experienced C++ experts be here, i have a
    > puzzle :
    >
    > (1) template <typename L, typename R, bool rL = false, bool rR =
    > flase > class A{.......};
    >
    > (2) template <typename L, typename R> class
    > A<L,R,true,true>{......};
    >
    > and there is a partial specification operator :
    > (3) template < >A<L,R>::eek:perator()(int){......};


    this is syntax error.

    >
    > int main(){
    > ......
    > return (new A<int,int,true,true>())->(5);
    > ...
    >
    > }
    >
    > in the main function, in the return sentence ......->(5);, we called
    > the operator(),
    > I think it should call class (2)'s or it's base class's operator (),
    > but the result is that it called the operator in (3).
    >


    basically, it will call the best match and specific one.
    well, the result indicates your (3) is best match and the most
    specific operator for A<int, int, true, true>..

    but you know, both compile and i doesn't understand the syntax error
    code.


    -roadt

    > Who can tell me some? thanks!
    Road.Tang, Jun 20, 2008
    #3
  4. Road.Tang Guest

    On Jun 20, 9:55 am, wrote:
    > Hi,
    > I know there are many experienced C++ experts be here, i have a
    > puzzle :
    >
    > (1) template <typename L, typename R, bool rL = false, bool rR =
    > flase > class A{.......};
    >
    > (2) template <typename L, typename R> class
    > A<L,R,true,true>{......};
    >
    > and there is a partial specification operator :
    > (3) template < >A<L,R>::eek:perator()(int){......};


    this is syntax error.

    >
    > int main(){
    > ......
    > return (new A<int,int,true,true>())->(5);
    > ...
    >
    > }
    >
    > in the main function, in the return sentence ......->(5);, we called
    > the operator(),
    > I think it should call class (2)'s or it's base class's operator (),
    > but the result is that it called the operator in (3).
    >


    basically, it will call the best match and specific one.
    well, the result indicates your (3) is best match and the most
    specific operator for A<int, int, true, true>..

    but you know, both compile and i doesn't understand the syntax error
    code.


    -roadt

    > Who can tell me some? thanks!
    Road.Tang, Jun 20, 2008
    #4
  5. Guest

    Hi, Eric

    Thanks for your suggestion very much! I paste it again:


    (1)
    template<typename T, typename L, bool b1=false, bool b2=false>
    struct A
    {
    A() {cout<<"1\n";}
    };


    (2)
    template <typename T, typename L>
    struct A<T, L,true,true>
    {
    A(){cout<<"2\n";}
    int operator()(int i);

    };



    (3)
    template<typename T, typename L>
    A<T,L>::eek:perator()(int i)
    {
    cout<< i<<endl;

    }




    int main()
    {
    return (new A<s,s,true,true>)->operator()(5);


    }


    this print
    2
    5


    In my opinion, I think the sentence (new A<s,s,true,true>)->operator()
    (5) will
    call the operator() in (2), but it call the explicit version in (3). I
    do not
    know why?
    , Jun 20, 2008
    #5
  6. Guest

    Hi, Eric and roadt, thanks your good suggestion!

    I paste it again.

    (1)
    template<typename L, typename R, bool b1=false, bool b2=false>
    struct A
    {
    A() {cout<<"1\n";}

    };

    (2)
    template <typename L, typename R>
    struct A<L, R, true, true>
    {
    A(){cout<<"2\n";}
    int operator()(int i);

    };

    (3)
    template< >
    A<int,int>::eek:perator()(int i)
    {
    cout<< i<<endl;

    }


    int main()
    {
    return (new A<int,int,true,true>)->operator()(5);


    }


    this print
    2
    5


    I think the setence
    (new A<int,int,true,true>)->operator()(5)

    will call the operator()(int) of (2).
    But it call the operator()(int) of (3), it is so strange.
    Because there are 2 dafault bool parameters in template(1).
    So I think the operator of (3) is equal to
    template< > A<int,int, false, false>::eek:perator()(int),
    of course the (new A<int,int,true,true>)->operator()(5)
    not match it.
    , Jun 20, 2008
    #6
  7. Hi,

    wrote:
    > I paste it again.
    >
    > (1)
    > template<typename L, typename R, bool b1=false, bool b2=false>
    > struct A
    > {
    > A() {cout<<"1\n";}
    >
    > };
    >
    > (2)
    > template <typename L, typename R>
    > struct A<L, R, true, true>
    > {
    > A(){cout<<"2\n";}
    > int operator()(int i);
    >
    > };
    >
    > (3)
    > template< >
    > A<int,int>::eek:perator()(int i)
    > {
    > cout<< i<<endl;
    >
    > }
    >
    >
    > int main()
    > {
    > return (new A<int,int,true,true>)->operator()(5);
    >
    >
    > }


    Your code still does not compile, since A<int,int,false,false> does not
    have operator()(int) defined. Furthermore this operator has no return
    value. Both are errors.

    So who came up with this output???

    > this print
    > 2
    > 5




    Marcel
    Marcel Müller, Jun 20, 2008
    #7
  8. Eric Pruneau Guest

    "Marcel Müller" <> a écrit dans le message de
    news: 485b9dba$0$6601$-online.net...
    > Hi,
    >
    > wrote:
    >> I paste it again.
    >>
    >> (1)
    >> template<typename L, typename R, bool b1=false, bool b2=false>
    >> struct A
    >> {
    >> A() {cout<<"1\n";}
    >>
    >> };
    >>
    >> (2)
    >> template <typename L, typename R>
    >> struct A<L, R, true, true>
    >> {
    >> A(){cout<<"2\n";}
    >> int operator()(int i);
    >>
    >> };
    >>
    >> (3)
    >> template< >
    >> A<int,int>::eek:perator()(int i)
    >> {
    >> cout<< i<<endl;
    >>
    >> }
    >>
    >>
    >> int main()
    >> {
    >> return (new A<int,int,true,true>)->operator()(5);
    >>
    >>
    >> }

    >
    > Your code still does not compile, since A<int,int,false,false> does not
    > have operator()(int) defined. Furthermore this operator has no return
    > value. Both are errors.


    this example compile and run fine with the intel compiler

    (1) does not need to have an operator(). the implementation of the
    specialization does not need to be related in any way to the generic
    definition.


    > So who came up with this output???


    me ( or at least my computer...)

    >
    >> this print
    >> 2
    >> 5

    >
    >
    >
    > Marcel


    ---------------

    Eric Pruneau
    Eric Pruneau, Jun 20, 2008
    #8
  9. Eric Pruneau Guest

    <> a écrit dans le message de news:
    ...
    >
    > Hi, Eric and roadt, thanks your good suggestion!
    >
    > I paste it again.
    >
    > (1)
    > template<typename L, typename R, bool b1=false, bool b2=false>
    > struct A
    > {
    > A() {cout<<"1\n";}
    >
    > };
    >
    > (2)
    > template <typename L, typename R>
    > struct A<L, R, true, true>
    > {
    > A(){cout<<"2\n";}
    > int operator()(int i);
    >
    > };
    >
    > (3)
    > template< >
    > A<int,int>::eek:perator()(int i)
    > {
    > cout<< i<<endl;
    >
    > }
    >
    >
    > int main()
    > {
    > return (new A<int,int,true,true>)->operator()(5);
    >
    >
    > }
    >
    >
    > this print
    > 2
    > 5
    >
    >
    > I think the setence
    > (new A<int,int,true,true>)->operator()(5)
    >
    > will call the operator()(int) of (2).


    Thats is exactly what it does

    > But it call the operator()(int) of (3), it is so strange.


    wait, (3) should be

    template< > A<int,int,true,true>::eek:perator()(int i) { ... }
    not
    template< > A<int,int>::eek:perator()(int i) { ... }

    the latter does not compile

    -------------------

    Eric Pruneau


    > Because there are 2 dafault bool parameters in template(1).
    > So I think the operator of (3) is equal to
    > template< > A<int,int, false, false>::eek:perator()(int),
    > of course the (new A<int,int,true,true>)->operator()(5)
    > not match it.
    >
    Eric Pruneau, Jun 20, 2008
    #9
  10. Eric Pruneau Guest

    "Eric Pruneau" <> a écrit dans le message de news:
    9DR6k.8917$...
    >
    > "Marcel Müller" <> a écrit dans le message de
    > news: 485b9dba$0$6601$-online.net...
    >> Hi,
    >>
    >> wrote:
    >>> I paste it again.
    >>>
    >>> (1)
    >>> template<typename L, typename R, bool b1=false, bool b2=false>
    >>> struct A
    >>> {
    >>> A() {cout<<"1\n";}
    >>>
    >>> };
    >>>
    >>> (2)
    >>> template <typename L, typename R>
    >>> struct A<L, R, true, true>
    >>> {
    >>> A(){cout<<"2\n";}
    >>> int operator()(int i);
    >>>
    >>> };
    >>>
    >>> (3)
    >>> template< >
    >>> A<int,int>::eek:perator()(int i)
    >>> {
    >>> cout<< i<<endl;
    >>>
    >>> }
    >>>
    >>>
    >>> int main()
    >>> {
    >>> return (new A<int,int,true,true>)->operator()(5);
    >>>
    >>>
    >>> }

    >>
    >> Your code still does not compile, since A<int,int,false,false> does not
    >> have operator()(int) defined. Furthermore this operator has no return
    >> value. Both are errors.

    >
    > this example compile and run fine with the intel compiler


    Wait, I wrote
    template< > A<int,in,true,truet>::eek:perator()(int i) { ...}

    for (3)

    I agree that like if you try to compile the example like it is, it will not
    compile.

    > (1) does not need to have an operator(). the implementation of the
    > specialization does not need to be related in any way to the generic
    > definition.
    >
    >
    >> So who came up with this output???

    >
    > me ( or at least my computer...)
    >
    >>
    >>> this print
    >>> 2
    >>> 5

    >>
    >>
    >>
    >> Marcel

    >
    > ---------------
    >
    > Eric Pruneau
    >
    Eric Pruneau, Jun 20, 2008
    #10
  11. Eric Pruneau Guest

    "Eric Pruneau" <> a écrit dans le message de news:
    bKR6k.6957$...
    >
    > "Eric Pruneau" <> a écrit dans le message de news:
    > 9DR6k.8917$...
    >>
    >> "Marcel Müller" <> a écrit dans le message de
    >> news: 485b9dba$0$6601$-online.net...
    >>> Hi,
    >>>
    >>> wrote:
    >>>> I paste it again.
    >>>>
    >>>> (1)
    >>>> template<typename L, typename R, bool b1=false, bool b2=false>
    >>>> struct A
    >>>> {
    >>>> A() {cout<<"1\n";}
    >>>>
    >>>> };
    >>>>
    >>>> (2)
    >>>> template <typename L, typename R>
    >>>> struct A<L, R, true, true>
    >>>> {
    >>>> A(){cout<<"2\n";}
    >>>> int operator()(int i);
    >>>>
    >>>> };
    >>>>
    >>>> (3)
    >>>> template< >
    >>>> A<int,int>::eek:perator()(int i)
    >>>> {
    >>>> cout<< i<<endl;
    >>>>
    >>>> }
    >>>>
    >>>>
    >>>> int main()
    >>>> {
    >>>> return (new A<int,int,true,true>)->operator()(5);
    >>>>
    >>>>
    >>>> }
    >>>
    >>> Your code still does not compile, since A<int,int,false,false> does not
    >>> have operator()(int) defined. Furthermore this operator has no return
    >>> value. Both are errors.

    >>
    >> this example compile and run fine with the intel compiler

    >
    > Wait, I wrote
    > template< > A<int,in,true,truet>::eek:perator()(int i) { ...}
    >
    > for (3)
    >
    > I agree that like if you try to compile the example like it is, it will
    > not compile.


    I meant will not link since operator() of (2) is not defined
    Eric Pruneau, Jun 20, 2008
    #11
    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. Marc Schellens

    template specification

    Marc Schellens, Oct 2, 2003, in forum: C++
    Replies:
    4
    Views:
    354
    Marc Schellens
    Oct 4, 2003
  2. Marek Vondrak
    Replies:
    9
    Views:
    321
    Marek Vondrak
    May 15, 2006
  3. Victor Bazarov

    Re: template specification oddness

    Victor Bazarov, Feb 21, 2008, in forum: C++
    Replies:
    3
    Views:
    304
    Matthias Buelow
    Feb 21, 2008
  4. Matthias Buelow

    template specification oddness

    Matthias Buelow, Feb 21, 2008, in forum: C++
    Replies:
    4
    Views:
    333
    James Kanze
    Feb 23, 2008
  5. Stephen Bannasch
    Replies:
    2
    Views:
    184
    Stephen Bannasch
    Dec 11, 2007
Loading...

Share This Page