473,779 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to avoid linkage problems with many function object classes

I have a bunch of function object classes of the form

struct MyFunc : unary_function< arg,ret>
{
MyFunc(arg) : state(arg) {}
void operator() (arg) { /*code here*/ }
};

in various cpp files. Now, the problem is that I happen to write two of
these with the same name in different modules that don't know anything
about each other, I will get a linker error, because the linker won't
be sure of what constructor to link against. How do I protect against
this?

I can't just mark a struct static, as that would only be indicating
that an instance of the struct (if specified) is static *data* and
*not* that the struct itself constructor and member functions itself
has module level linkage only. Is there an actual good way to handle
this?

Nov 28 '06 #1
3 1320
On 28 Nov 2006 15:19:42 -0800 in comp.lang.c++, ca************@ gmail.com
wrote,
>I have a bunch of function object classes of the form
namespace {
>struct MyFunc : unary_function< arg,ret>
{
MyFunc(arg) : state(arg) {}
void operator() (arg) { /*code here*/ }
};
};
Nov 28 '06 #2
ca************@ gmail.com wrote:
I have a bunch of function object classes of the form

struct MyFunc : unary_function< arg,ret>
{
MyFunc(arg) : state(arg) {}
void operator() (arg) { /*code here*/ }
};

in various cpp files. Now, the problem is that I happen to write two of
these with the same name in different modules that don't know anything
about each other, I will get a linker error, because the linker won't
be sure of what constructor to link against. How do I protect against
this?
Put the struct definitions in the anonymous namespace.
I can't just mark a struct static, as that would only be indicating
that an instance of the struct (if specified) is static *data* and
*not* that the struct itself constructor and member functions itself
has module level linkage only. Is there an actual good way to handle
this?
If you add the static keyword to the beginning of a struct definition,
that means that the struct has internal linkage. This is the old C way
of doing this. The anonymous namespace is the preferred C++ way doing this.

Nate
Nov 28 '06 #3
Nate Barney wrote:
>
If you add the static keyword to the beginning of a struct definition,
that means that the struct has internal linkage. This is the old C way
of doing this. The anonymous namespace is the preferred C++ way doing
this.
My mistake. I just checked, and the static keyword can only be applied
in this manner to objects and functions.

Nate
Nov 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
12144
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. // test_const.h #ifndef HEADER_TEST #define HEADER_TEST
4
10008
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require the extern "C" declaration, for correct linkage into a C++ program, don't need it if the filename, in which they are defined, ends in .cc or .cpp! Can anyone explain this in a rational manner? For the record, this is what I have heard...
20
3154
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
22
6030
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
10
6198
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
19
2918
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single program.
5
2095
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
5
2270
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
4
1661
by: Bart Simpson | last post by:
I m getting a ton of linkage errors (unresolved externals) when using an abstract base class. My classes look something like this: class BaseObject { //pure virtuals virtual void foo() = 0; virtual int foobar(int, int, double) = 0 ; virtual void grok();
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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
10306
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
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
6724
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
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.