static member function in class template

Discussion in 'C++' started by Martin Vorbrodt, Oct 6, 2005.

  1. SO i have a class:

    template<typename T>
    class Vector {
    public:
    static const Vector<T> UNIT_X();
    };

    template<typename T>
    static const Vector<T> UNIT_X() {
    static const Vector<T> unit_x(1,0,0);
    return unit_x;
    }

    GCC 3.4 tell me i can't declare UNIT_X member function to have static
    linkage.

    How do i go about having such static function.

    I want to be able to do Vector<int>::UNIX_X();

    Please help.

    Martin
    Martin Vorbrodt, Oct 6, 2005
    #1
    1. Advertising

  2. Martin Vorbrodt

    mlimber Guest

    Martin Vorbrodt wrote:
    > SO i have a class:
    >
    > template<typename T>
    > class Vector {
    > public:
    > static const Vector<T> UNIT_X();
    > };
    >
    > template<typename T>
    > static const Vector<T> UNIT_X() {


    Try this line instead:

    const Vector<T> Vector<T>::UNIT_X() {

    > static const Vector<T> unit_x(1,0,0);
    > return unit_x;
    > }
    >
    > GCC 3.4 tell me i can't declare UNIT_X member function to have static
    > linkage.
    >
    > How do i go about having such static function.
    >
    > I want to be able to do Vector<int>::UNIX_X();
    >
    > Please help.
    >
    > Martin


    Cheers! --M
    mlimber, Oct 6, 2005
    #2
    1. Advertising

  3. of course, thanks! silly me.

    now another thing. the static function must be defined in the header file as
    well right? doesn't that violate ODR?

    martin



    "mlimber" <> wrote in message
    news:...
    >
    > Martin Vorbrodt wrote:
    > > SO i have a class:
    > >
    > > template<typename T>
    > > class Vector {
    > > public:
    > > static const Vector<T> UNIT_X();
    > > };
    > >
    > > template<typename T>
    > > static const Vector<T> UNIT_X() {

    >
    > Try this line instead:
    >
    > const Vector<T> Vector<T>::UNIT_X() {
    >
    > > static const Vector<T> unit_x(1,0,0);
    > > return unit_x;
    > > }
    > >
    > > GCC 3.4 tell me i can't declare UNIT_X member function to have static
    > > linkage.
    > >
    > > How do i go about having such static function.
    > >
    > > I want to be able to do Vector<int>::UNIX_X();
    > >
    > > Please help.
    > >
    > > Martin

    >
    > Cheers! --M
    >
    Martin Vorbrodt, Oct 6, 2005
    #3
  4. Martin Vorbrodt wrote:
    > of course, thanks! silly me.
    >
    > now another thing. the static function must be defined in the header file as
    > well right? doesn't that violate ODR?


    templates are an exception to ODR.

    john
    John Harrison, Oct 6, 2005
    #4
  5. John Harrison wrote:
    > Martin Vorbrodt wrote:
    > > of course, thanks! silly me.
    > >
    > > now another thing. the static function must be defined in the header file as
    > > well right? doesn't that violate ODR?

    >
    > templates are an exception to ODR.
    >
    > john


    what is ODR (one definition rule)?
    puzzlecracker, Oct 7, 2005
    #5
  6. Martin Vorbrodt

    Guest

    || what is ODR (one definition rule)?
    Yep!!
    , Oct 7, 2005
    #6
  7. wrote:
    > || what is ODR (one definition rule)?
    > Yep!!


    I don't think "Yep" is a definition. Get a life -trol!
    puzzlecracker, Oct 7, 2005
    #7
  8. Even static members of templates? Well, I believe it, the code works, but...
    feels dirty. Static functions definitions in header files... I guess export
    would fix that. Too bad my compiler doesn't this so;)

    Anyone knows a 100% conforming compiler? Is there one in works?

    Thanx



    "John Harrison" <> wrote in message
    news:JEe1f.10302$...
    > Martin Vorbrodt wrote:
    >> of course, thanks! silly me.
    >>
    >> now another thing. the static function must be defined in the header file
    >> as
    >> well right? doesn't that violate ODR?

    >
    > templates are an exception to ODR.
    >
    > john
    Martin Vorbrodt, Oct 7, 2005
    #8
  9. Martin Vorbrodt

    Kai-Uwe Bux Guest

    a) please don't top post.

    Martin Vorbrodt wrote:

    > Even static members of templates? Well, I believe it, the code works,
    > but... feels dirty. Static functions definitions in header files...


    Why dirty? I consider support for separate compilation more an issue of
    speeding up the built process and not that much an issue of code
    organization.


    > I guess export would fix that. Too bad my compiler doesn't this so;)
    >
    > Anyone knows a 100% conforming compiler? Is there one in works?


    Check out http://www.comeaucomputing.com/


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 7, 2005
    #9
    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. 0to60
    Replies:
    4
    Views:
    502
    jeffc
    Nov 21, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    785
    Micah Cowan
    Jan 12, 2005
  3. aling
    Replies:
    6
    Views:
    466
    Xiaobin.Huang
    Oct 30, 2005
  4. Frederiek
    Replies:
    1
    Views:
    369
    Thomas Tutone
    Sep 14, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page