Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:59+00:00 2026-05-29T09:45:59+00:00

I have some linking issues which I do not understand. Here are the source:

  • 0

I have some linking issues which I do not understand.

Here are the source:

#include <iostream>

template < typename T >
struct CompressedEnums {
    CompressedEnums () : data(0) {}

    T get() const {
        return (T)(data);
    }

    void set(const T& value) {
        data = value;
    }

    unsigned data;
};

namespace Bbs_detail {
    enum inner_type { BB0 = 0 , BB1 = 1 , BB2 = 2 };
    typedef inner_type E;
};

struct Bbs {
    static const size_t size = 3;
    typedef Bbs_detail::inner_type inner_type;
    typedef inner_type E;
    static const Bbs_detail::E BB0 = Bbs_detail::BB0;
    static const Bbs_detail::E BB1 = Bbs_detail::BB1;
    static const Bbs_detail::E BB2 = Bbs_detail::BB2;
};

std::ostream& operator<<(std::ostream& o, const Bbs::E& e) {
    switch(e) {
        case Bbs::BB0: o << "BB0"; return o;
        case Bbs::BB1: o << "BB1"; return o;
        case Bbs::BB2: o << "BB2"; return o;
    }
    return o;
};

int main(int argc, const char *argv[]) {
    CompressedEnums< Bbs::E > l;

    l.set(Bbs::BB0);
    Bbs::E x = l.get();

    std::cout << x << std::endl;

    return 0;
}

When I compile this with -O3 it works, but I get linker errors with -O0. Ih have tried both with gcc 4.6.2 and and gcc 4.7.

When compiling with clang 3.0 I get linker errors regardless of the optimization level.

Linker errors:

/tmp/cch116DO.o: In function `main':
test.cxx:(.text+0x8f): undefined reference to `Bbs::BB0'
collect2: error: ld returned 1 exit status

Is this because I’m doing something illegal?

I was of the opinion that static const members of integral types can be initialized in-class, isn’t that right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:46:00+00:00Added an answer on May 29, 2026 at 9:46 am

    Citing the C++98 standard, 9.4.2 Static data members:

    If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant. The member shall still be defined in a namespace scope if it is used in the program and the namespace
    scope definition shall not contain an initializer. [emphasis mine].

    So you have to define the constant members is Alan Stokes pointed in his answer. If you don’t do that, and the compiler manages to avoid any reference to the variable, because it is constant and it already knows the value, you may do without it. But no guarantees about that.

    Curiously enough in the C++11 draft, there are a few additional notes about constexpr and brace initializers, and then:

    The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program.

    Then, in point 3.2 it defines what odr-used means:

    A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied.

    That is, in C++11 you have the guarantee not to need the member definition if all your uses of the constant member are in constant expressions:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some strange linking problem in my Visual Studio 2005 C++ project. As
I have some ASP.NET web services which all share a common helper class they
I have some C# / asp.net code I inherited which has a textbox which
I have a program to which I'm adding fullscreen post-processing effects. I do not
I have written some code that makes use of an open source library to
I have a script called 'sess-start.php' which lies in an /include directory within my
I'm kinda having some issues with linking my assembly. I use NASM for assembly
I have some UI in VB 2005 that looks great in XP Style, but
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
We have some input data that sometimes appears with &nbsp characters on the end.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.