http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57588

            Bug ID: 57588
           Summary: [C++11][constexpr] static constexpr in class fails to
                    link
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mattyclarkson at gmail dot com

The following code, links in optimisation modes but not others:

  #include <iostream>

 
///////////////////////////////////////////////////////////////////////////////
  class Literal {
  private:
    Literal() = delete;
    Literal(const Literal&) = delete;
    Literal(Literal&&) = delete;
    Literal& operator=(const Literal&) = delete;
    Literal& operator=(Literal&&) = delete;
    ~Literal() = default;

  public:
    constexpr Literal(const int i);

  public:
    constexpr operator int() const;

  private:
    int int_;
  };

  constexpr Literal::Literal(const int i) : int_(i) {};

  constexpr Literal::operator int() const {
    return int_;
  }

 
///////////////////////////////////////////////////////////////////////////////
  class Test {
  private:
    Test(const Test&) = delete;
    Test(Test&&) = delete;
    Test& operator=(const Test&) = delete;
    Test& operator=(Test&&) = delete;
    ~Test() = default;

  public:
    static constexpr const Literal kLiteral = Literal(99);

  public:
    constexpr Test();

  public:
    constexpr operator int() const;

  private:
    int int_;
  };

  constexpr Test::Test() : int_(kLiteral) {};

  constexpr Test::operator int() const {
    return int_;
  }

 
///////////////////////////////////////////////////////////////////////////////
  int main() {
    std::cout << Literal(15) << std::endl;
    std::cout << Test() << std::endl;
    return 0;
  }

Has some odd compilation on my machine:

  [matt test] uname -a
  Linux office.acer-m5810 3.8.13-100.fc17.x86_64 #1 SMP Mon May 13 13:36:17 UTC
2013 x86_64 x86_64 x86_64 GNU/Linux
  [matt test] g++ -std=c++11 test.cpp -o test -O0 -g && ./test
  /tmp/ccZtV9ku.o: In function `Test::Test()':
  /home/matt/test/test.cpp:51: undefined reference to `Test::kLiteral'
  collect2: error: ld returned 1 exit status
  [matt test] g++ -std=c++11 test.cpp -o test -O1 -g && ./test
  15
  99
  [matt test] g++ -std=c++11 test.cpp -o test -O2 -g && ./test
  15
  99
  [matt test] g++ -std=c++11 test.cpp -o test -O3 -g && ./test
  15
  99

However, on gcc.godbolt.org and ideone.com I cannot get the same compilation
error with 4.7.2.  I'm assuming I'm not being a donkey and doing something
against the standard? :/

If I can help with any debugging, please ask.

Reply via email to