Help with explicit instantiation

Discussion in 'C++' started by E. Robert Tisdale, Sep 18, 2004.

  1. > cat X.h
    #ifndef GUARD_X_H
    #define GUARD_X_H 1
    #include <iostream>

    template<class T>
    class X {
    private:
    // representation
    T D;
    public:
    // functions
    T data(void) const { return D; }
    // constructors
    X(T d = 0): D(d) { }
    X(const X& x);
    template<class S>
    X(const X<S>& x);
    friend
    std::eek:stream& operator<<(
    std::eek:stream& os, const X<T>& x) {
    return os << x.data();
    }
    };

    #endif//GUARD_X_H 1

    > cat X.cc

    #include "X.h"

    template<class T>
    X<T>::X(const X& x): D(x.D) {
    std::cerr << "copy constructor" << std::endl;
    }

    template<class T>
    template<class S>
    X<T>::X(const X<S>& x): D(x.data()) {
    std::cerr << "conversion constructor" << std::endl;
    }

    template X<float>::X(const X<int>& x);
    template X<float>::X(const X<float>& x);

    > g++ -Wall -ansi -pedantic -c X.cc

    X.cc:15: error: ambiguous template specialization `X<>' \
    for `X<float>::X(const X<float>&)'
    X.cc:4: error: candidates are: \
    X<T>::X(const X<T>&) [with T = float]
    X.h:17: error: \
    template<class S> X::X(const X<S>&) [with S = S, T = float]

    What did I do wrong?
     
    E. Robert Tisdale, Sep 18, 2004
    #1
    1. Advertising

  2. E. Robert Tisdale

    Sumit Rajan Guest

    "E. Robert Tisdale" <> wrote in message
    news:cifs1d$ls7$...

    > What did I do wrong?


    Seems to compile without errors/warnings on Comeau C++ 4.3.3 and MSVC++ 7.1.

    Regards,
    Sumit.
    --
    Sumit Rajan <sumitrajan AT alexandria DOT cc>
     
    Sumit Rajan, Sep 18, 2004
    #2
    1. Advertising

  3. Sumit Rajan wrote:

    > E. Robert Tisdale wrote:
    >
    >>What did I do wrong?

    >
    > Seems to compile without errors/warnings on Comeau C++ 4.3.3 and MSVC++ 7.1.


    I just tried it with my SGI MIPSpro C++ compiler
    and it compiles without complaint there as well.

    I guess that it's a GNU C++ compiler bug.

    Can anybody suggest a work-around?
     
    E. Robert Tisdale, Sep 18, 2004
    #3
  4. E. Robert Tisdale wrote:
    >>> What did I do wrong?


    Hi,
    it seems to be very old bug - look at
    http://gcc.gnu.org/ml/gcc-prs/2000-q3/msg00288.html

    To workaround it maybe try to use extra parameter: X<T>(X<S>, int).
    The following code compiles well with gcc-3.4.

    Yevgen


    /////////////////////////////////////////////////
    // X.h

    #ifndef GUARD_X_H
    #define GUARD_X_H 1
    #include <iostream>

    template<class T>
    class X {
    private:
    // representation
    T D;
    public:
    // functions
    T data(void) const { return D; }
    // constructors
    X(T d = 0): D(d) { }
    X(const X& x);
    template<class S>
    X(const X<S>& x, int i = 0);
    friend
    std::eek:stream& operator<<(std::eek:stream& os, const X<T>& x) {
    return os << x.data();
    }
    };

    #endif//GUARD_X_H 1

    ///////////////////////////////////////////
    // X.cc

    #include "X.h"

    template<class T>
    X<T>::X(const X& x): D(x.D)
    {
    std::cerr << "copy constructor" << std::endl;
    }

    template<class T>
    template<class S>
    X<T>::X(const X<S>& x, int i): D(x.data())
    {
    std::cerr << "conversion constructor" << std::endl;
    }


    template X<float>::X(const X<int>& x, int);
    template X<float>::X(const X<float>& x);

    void f()
    {
    X<float> a(X<float>(1.0));
    X<float> b(X<int>(1));
    }
     
    Yevgen Muntyan, Sep 18, 2004
    #4
    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. Rolf Magnus
    Replies:
    0
    Views:
    851
    Rolf Magnus
    Jul 23, 2003
  2. C. Carbonera
    Replies:
    4
    Views:
    1,348
    C. Carbonera
    Feb 5, 2004
  3. Fernando Cuenca
    Replies:
    4
    Views:
    2,529
    Gianni Mariani
    Sep 6, 2004
  4. BigMan
    Replies:
    1
    Views:
    341
    Victor Bazarov
    Apr 28, 2005
  5. Replies:
    1
    Views:
    579
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page