how to avoid linkage problems with many function object classes

Discussion in 'C++' started by catphive.lists@gmail.com, Nov 28, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. David Harmon Guest

    On 28 Nov 2006 15:19:42 -0800 in comp.lang.c++,
    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*/ }
    >};


    };
     
    David Harmon, Nov 28, 2006
    #2
    1. Advertising

  3. Nate Barney Guest

    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
     
    Nate Barney, Nov 28, 2006
    #3
  4. Nate Barney Guest

    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
     
    Nate Barney, Nov 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Ford
    Replies:
    2
    Views:
    604
    Michael Meyer
    Jan 29, 2004
  2. tropos
    Replies:
    3
    Views:
    478
  3. Bart Simpson
    Replies:
    4
    Views:
    318
    James Kanze
    Jun 3, 2007
  4. Replies:
    1
    Views:
    606
    Michael DOUBEZ
    Sep 12, 2008
  5. Jim Thomason
    Replies:
    3
    Views:
    174
    Topmind
    Oct 2, 2004
Loading...

Share This Page