Defining globals inside structs

C

chrisstankevitz

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;
}
 
V

Victor Bazarov

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
 
C

chrisstankevitz

Victor said:
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
 
V

Victor Bazarov

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:

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
 
C

chrisstankevitz

Victor said:
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
 
P

Phlip

chrisstankevitz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top