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;
} 5 1741 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
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 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
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
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!!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
I get the impression that most people who are using objects in their
PHP projects are mixing them with procedural code. The design, I
think, is one where the procedural code is the client code, and...
|
by: John |
last post by:
Hi,
I'm looking for the best way to deal with globals in PHP.
As a 'C' software developer, I would normally avoid all
globals and not have any at all, but use structs and pass
everything in...
|
by: Robert Dodier |
last post by:
Hello,
I'm interested in introducing new variables into the environment
of a Python interpreter or program.
In reading through old posts to this newsgroup, I see there is
an often-repeating...
|
by: DFS |
last post by:
Not sure whether it's bad programming practice or not, but I have a module
of globals I declare in each system:
Global ws As Workspace
Global db As Database
Global td As TableDef
Global rs As...
|
by: mblatch |
last post by:
Another basic C# question, but haven't figured out how to do this one
either. Am used to defining a list of structures in C++, but am unsure
if you can do this in C#. As a simplified example, I...
|
by: thomson |
last post by:
Hi all,
Would you please explain me where will be the heap stored if it
is declared inside the Class, As class is a reference type, so it gets
stored on the heap, but struct is a value...
|
by: carled |
last post by:
Hi all. New here and new to .net from classic asp. I have to access an xml-rpc webservice in .net and it's making my brain fry trying to get it all set up. I'm most comfortable with vb, but I can...
|
by: Gabriel Genellina |
last post by:
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <akathorn@gmail.com>
escribi�:
Welcome!
You have to pass in the namespace of the desired module - instead of
globals. I'd use an...
|
by: r0g |
last post by:
Hi There,
I'm refactoring some old code that uses global variables and was
originally written in one big flat file with a view to nicening it up
and then extending it. The problem I have though...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |