Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to avoid linkage problems with many function object classes

Reply
Thread Tools

how to avoid linkage problems with many function object classes

 
 
catphive.lists@gmail.com
Guest
Posts: n/a
 
      11-28-2006
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?

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      11-28-2006
On 28 Nov 2006 15:19:42 -0800 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail Removed)
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*/ }
>};


};


 
Reply With Quote
 
 
 
 
Nate Barney
Guest
Posts: n/a
 
      11-28-2006
(E-Mail Removed) 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
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      11-28-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
ABCs (abstract base classes) and linkage problems Bart Simpson C++ 4 06-03-2007 08:54 PM
Problem: shared object loading runs constructor of a static object, but static linkage does not. tropos C++ 3 11-30-2005 04:54 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments