By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

Defining globals inside structs

P: n/a

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 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ch*************@yahoo.com 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
Apr 10 '06 #2

P: n/a
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>::operator*(const C<T>& p, T d)
{
}

Thanks again for your help,

Chris

Apr 10 '06 #3

P: n/a
ch*************@yahoo.com 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>::operator*(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
Apr 11 '06 #4

P: n/a

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 '06 #5

P: n/a
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!!!
Apr 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.