473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining globals inside structs


Why does this code only compile if GLOBAL_IN_STRUC T 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_STRUC T

template <typename T>
struct C {
#ifdef GLOBAL_IN_STRUC T
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
5 1768
ch************* @yahoo.com wrote:
Why does this code only compile if GLOBAL_IN_STRUC T 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_STRUC T

template <typename T>
struct C {
#ifdef GLOBAL_IN_STRUC T
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
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*<dou ble>(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
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*<doubl e>(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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3849
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 the classes are a library of utility code. As such, when I've asked about how to get globals into objects, I've been told that I should pass them in as the parameters to a method. Easy enough if you have some procedural code. This answer I've...
7
1761
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 the function parameters... However, being realistic, I can see that globals can (and do ?) have a place in PHP web scripts.
3
2078
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 warning against modifying the contents of locals(). Fair enough. However, is there anything wrong with modifying globals() ?
2
1654
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 Recordset, rs1 As Recordset Global rs2 As Recordset, rs3 As Recordset Global ctl As Control Global frm As Form, subFrm As Form
5
38371
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 have: struct Command { int commandEnum; string commandName; int commandLength; byte command;
9
2678
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 type-stored on the stack Regards thomson
1
1999
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 translate from c# if anyone can do it in that! I have created a class in my app_code folder that will access the webservice like so: Imports Microsoft.VisualBasic Imports CookComputing.XmlRpc Public Class mapserver Public Structure...
0
1047
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 explicit argument:
3
1992
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 is when I move the various classes out to their own separate files and reimport them back in they can't see the globals in the main program. Most of the globals were just constants and so I have been able to factor them out but there's one that's...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.