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

how to avoid linkage problems with many function object classes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.