Conversion Operator in Template Class

Discussion in 'C++' started by joecook@gmail.com, Mar 18, 2009.

  1. Guest

    Hmm... Why doesn't this work and use the conversion operator? (It
    doesn't compile)

    template<typename T>
    class Foo
    {
    };

    template<typename T>
    void bar(Foo<T>& a){}

    template<typename T>
    class Goo
    {
    public:
    operator Foo<U>(){return m_foo;}

    Foo<U> m_foo;
    };

    int main()
    {
    Goo<float> g;
    bar(g); // why doesn't this convert to Foo<U> ??
    }

    Thanks
    , Mar 18, 2009
    #1
    1. Advertising

  2. Guest

    On Mar 18, 11:27 am, Victor Bazarov <> wrote:
    > wrote:
    > > Hmm... Why doesn't this work and use the conversion operator?  (It
    > > doesn't compile)

    > Victor said:
    > Because the Standard prohibits it.  When deducing template arguments
    > from a function call, the conversions are not applied to the function
    > arguments.



    Is there any way to get similar capability that you know of? What if
    i wanted to have a class that behaved in everyway as say a
    std::vector<T>, but I didn't want to inherit from std::vector<T> ? I
    still want to pass this new class to functions that accept
    std::vector<T>

    Joe C
    , Mar 18, 2009
    #2
    1. Advertising

  3. SG Guest

    On 18 Mrz., 16:27, Victor Bazarov <> wrote:
    > wrote:
    > > Hmm... Why doesn't this work and use the conversion operator?  (It
    > > doesn't compile)

    >
    > > template<typename T>
    > > class Foo
    > > {
    > > };

    >
    > > template<typename T>
    > >   void bar(Foo<T>& a){}

    >
    > > template<typename T>
    > > class Goo
    > > {
    > > public:
    > >   operator Foo<U>(){return m_foo;}

    >
    > >  Foo<U> m_foo;
    > > };

    >
    > > int main()
    > > {
    > > Goo<float> g;
    > >  bar(g); // why doesn't this convert to Foo<U> ??
    > > }

    >
    > Because the Standard prohibits it.  When deducing template arguments
    > from a function call, the conversions are not applied to the function
    > arguments.


    In addition: The conversion would result in an rvalue which is not
    allowed to bind to an lvalue reference to non-const.

    Cheers!
    SG
    SG, Mar 18, 2009
    #3
  4. James Kanze Guest

    On Mar 18, 4:12 pm, wrote:
    > Hmm... Why doesn't this work and use the conversion operator?
    > (It doesn't compile)


    > template<typename T>
    > class Foo
    > {
    > };


    > template<typename T>
    > void bar(Foo<T>& a){}


    > template<typename T>
    > class Goo
    > {
    > public:
    > operator Foo<U>(){return m_foo;}


    What's U?

    > Foo<U> m_foo;


    Same question?

    > };


    > int main()
    > {
    > Goo<float> g;
    > bar(g); // why doesn't this convert to Foo<U> ??
    > }


    Your class template Goo shouldn't compile, since there is no
    symbol U defined anywhere. If you replace the U with T, it
    still won't compile, because the compiler doesn't know which
    specialization of bar to use, and it can't do overload
    resolution before bar has been specialized.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 19, 2009
    #4
  5. James Kanze Guest

    On Mar 18, 6:59 pm, Victor Bazarov <> wrote:
    > wrote:
    > > On Mar 18, 11:27 am, Victor Bazarov <> wrote:
    > >> wrote:
    > >>> Hmm... Why doesn't this work and use the conversion operator? (It
    > >>> doesn't compile)
    > >> Victor said:
    > >> Because the Standard prohibits it. When deducing template
    > >> arguments from a function call, the conversions are not
    > >> applied to the function arguments.


    > > Is there any way to get similar capability that you know of? What if
    > > i wanted to have a class that behaved in everyway as say a
    > > std::vector<T>, but I didn't want to inherit from std::vector<T> ? I
    > > still want to pass this new class to functions that accept
    > > std::vector<T>


    > Not in templates; you'd have to be explicit, and introduce a cast or a
    > function, like "as_vector":


    > template<class T> class Goo {
    > ...
    > Foo<T>& as_Foo() { return m_foo; }
    > };


    > Goo<float> g;
    > bar(g.as_Foo());


    > . I am not sure why such requirements are imposed, probably
    > to allow compilers actually be reasonable with respect to time
    > they take to compile templates.


    If by reasonable, you mean finite, yes. If you allowed all of
    the conversions allowed for non template functions, in some
    cases, you would end up with an infinite set of possibilities to
    evaluate.

    > Imagine how many functions/class would have to be instantiated
    > if the compiler started applying all possible conversions,
    > especially in the cases with built-in types...


    Type deduction always yields a single function (for a given
    function template). What you would likely end up in such cases
    is a large number of equally good choices, which no way to
    choose (ambiguous), or in some extreme cases, the necessity to
    check an infinite set of possibilities just in case one was
    better.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 19, 2009
    #5
    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. christopher diggins
    Replies:
    16
    Views:
    722
    Pete Becker
    May 4, 2005
  2. Arvid Requate
    Replies:
    2
    Views:
    962
    Alf P. Steinbach
    Jun 23, 2006
  3. hurcan solter
    Replies:
    3
    Views:
    708
    Cholo Lennon
    Aug 29, 2007
  4. , India
    Replies:
    2
    Views:
    436
    Fraser Ross
    Sep 15, 2009
  5. A L
    Replies:
    1
    Views:
    493
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page