Defining globals inside structs

Discussion in 'C++' started by chrisstankevitz@yahoo.com, Apr 10, 2006.

  1. Guest

    Why does this code only compile if GLOBAL_IN_STRUCT is defined?

    It creates a templated class C<T> and defines a global operator* that
    takes a C<T> on the LHS and a T on the RHS.

    In the example, T is double, but I call the global operator* with a
    float.

    Thanks for your help,

    Chris


    //-------------------------------------
    //-------------------------------------


    #define GLOBAL_IN_STRUCT

    template <typename T>
    struct C {
    #ifdef GLOBAL_IN_STRUCT
    friend void operator*(const C<T>& p, T d) {}
    };
    #else
    };
    template <typename T> void operator*(const C<T>& p, T d) {}
    #endif

    int main()
    {
    C<double> v;

    v * 0.0f;

    return 0;
    }
    , Apr 10, 2006
    #1
    1. Advertising

  2. wrote:
    > Why does this code only compile if GLOBAL_IN_STRUCT is defined?
    >
    > It creates a templated class C<T> and defines a global operator* that
    > takes a C<T> on the LHS and a T on the RHS.
    >
    > In the example, T is double, but I call the global operator* with a
    > float.
    >
    > Thanks for your help,
    >
    > Chris
    >
    >
    > //-------------------------------------
    > //-------------------------------------
    >
    >
    > #define GLOBAL_IN_STRUCT
    >
    > template <typename T>
    > struct C {
    > #ifdef GLOBAL_IN_STRUCT
    > friend void operator*(const C<T>& p, T d) {}


    When you define the operator here, 'T' is taken from the template
    instantiation and doesn't need to be deduced.

    > };
    > #else
    > };
    > template <typename T> void operator*(const C<T>& p, T d) {}
    > #endif
    >
    > int main()
    > {
    > C<double> v;
    >
    > v * 0.0f;
    >
    > return 0;
    > }


    If you don't limit the scope of the operator* to C<>, then the compiler
    has to figure out the 'T' from the invocation. On one hand, from the
    first argument, 'T' needs to be 'double'. But from the second artument,
    'T' needs to be 'float'. That's the conflict the compiler cannot resolve.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 10, 2006
    #2
    1. Advertising

  3. Guest

    Victor Bazarov wrote:
    > If you don't limit the scope of the operator* to C<>, then the compiler
    > has to figure out the 'T' from the invocation. On one hand, from the
    > first argument, 'T' needs to be 'double'. But from the second artument,
    > 'T' needs to be 'float'. That's the conflict the compiler cannot resolve.
    >


    Victor,

    Thanks for your reply, it makes sense to me. I see that I can use my
    non-scope-limited operator* by specifying the template argument:
    ::eek:perator*<double>(v, 0.0f);

    Assuming I want operator* scope limited to C<>, I will do this:

    template <typename T>
    struct C {
    friend void operator*(const C<T>& p, T d) {}
    };

    What is the syntax to define the function outside the struct (but
    declare it inside)? The following does not work (not surprising):

    template <typename T>
    struct C {
    friend void operator*(const C<T>& p, T d);
    };

    template <typename T>
    inline void C<T>::eek:perator*(const C<T>& p, T d)
    {
    }

    Thanks again for your help,

    Chris
    , Apr 10, 2006
    #3
  4. wrote:
    > Victor Bazarov wrote:
    >> If you don't limit the scope of the operator* to C<>, then the
    >> compiler has to figure out the 'T' from the invocation. On one
    >> hand, from the first argument, 'T' needs to be 'double'. But from
    >> the second artument, 'T' needs to be 'float'. That's the conflict
    >> the compiler cannot resolve.
    >>

    >
    > Victor,
    >
    > Thanks for your reply, it makes sense to me. I see that I can use my
    > non-scope-limited operator* by specifying the template argument:
    >>> operator*<double>(v, 0.0f);

    >
    > Assuming I want operator* scope limited to C<>, I will do this:
    >
    > template <typename T>
    > struct C {
    > friend void operator*(const C<T>& p, T d) {}
    > };
    >
    > What is the syntax to define the function outside the struct (but
    > declare it inside)? The following does not work (not surprising):
    >
    > template <typename T>
    > struct C {
    > friend void operator*(const C<T>& p, T d);
    > };
    >
    > template <typename T>
    > inline void C<T>::eek:perator*(const C<T>& p, T d)
    > {
    > }
    >
    > Thanks again for your help,


    There is no way to do what you want by declaring it inside and defining
    outside. Since the definition is going to be a separate template, the
    compiler will always be forced to figure out the template argument for
    it, and it will fail, just as if you don't declare it inside.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Apr 11, 2006
    #4
  5. Guest

    Victor Bazarov wrote:
    > There is no way to do what you want by declaring it inside and defining
    > outside.


    That's interesting. Part of our style guide is to not define functions
    inside the struct/class. (i.e. inline definitions go outside the class
    at the bottom of the .h file). Apparently this is not a good rule
    because the example above shows a case where it is impossible to follow
    the rule.

    Also, the "friend" notation is unusual to me. I never thought I'd need
    a friend when all elements in a struct are public. I need to go read
    up on what it means to be a friend.

    Thanks,

    Chris
    , Apr 11, 2006
    #5
  6. Phlip Guest

    chrisstankevitz wrote:

    > That's interesting. Part of our style guide is to not define functions
    > inside the struct/class. (i.e. inline definitions go outside the class
    > at the bottom of the .h file). Apparently this is not a good rule
    > because the example above shows a case where it is impossible to follow
    > the rule.


    That's the meaning of a style guide: Things to do without a technical reason
    to do them any other way.

    Write your code, and put a comment "// this is inside the function
    because..." then explain the language law involved.

    > Also, the "friend" notation is unusual to me. I never thought I'd need
    > a friend when all elements in a struct are public. I need to go read
    > up on what it means to be a friend.


    In this case, it means the defined function is part of that class's
    interface, even though it's not a member function.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    Phlip, Apr 11, 2006
    #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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    611
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    539
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    626
    Eric Sosman
    Oct 11, 2005
  4. Sur
    Replies:
    4
    Views:
    168
  5. Steven D'Aprano
    Replies:
    5
    Views:
    76
    Rotwang
    Nov 14, 2013
Loading...

Share This Page