2

I recently tested the simple following program on an online compiler. See live demo here. It compiles fine & gives the expected output but when I tested it on Dev C++ IDE it fails during the compilation.

Here is my program:

#include <iostream>
class Test
{
    int s=9;
    public:
    int get_s()
    {
        return s;
    }
};
int main()
{
    Test s;
    Test& t{s};      // error in C++11 but not in C++14 why???
    std::cout<<t.get_s();
} 

It gives me following error:

[Error] invalid initialization of non-const reference of type 'Test&' from an rvalue of type '<brace-enclosed initializer list>'

I also tried it on code blocks 13.12 IDE & it gives me the same error as Dev C++ gives.

Is this a new C++14 feature? Why is it not working in a C++11 compiler?

Marco A.
  • 43,032
  • 26
  • 132
  • 246
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 1
    [It is working](http://coliru.stacked-crooked.com/a/4fbf7065dd424257). Perhaps something missing in a C++11-incomplete compiler? – Marco A. Jun 06 '15 at 08:42
  • @MarcoA. yes, it is working in C++14 compiler but not working in C++11 compiler. Have you tried this program in C++0x compiler? – Destructor Jun 06 '15 at 08:44
  • 3
    @meet Did you notice that the link uses `-std=c++11`, not `-std=c++14`? –  Jun 06 '15 at 08:46
  • @hvd: oh sorry just noticed that after you said. But it fails in compilation on my compiler. – Destructor Jun 06 '15 at 08:48
  • 2
    Perhaps related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025. Is mingw/gcc the underlying compiler? If so: which version? – Marco A. Jun 06 '15 at 08:48
  • 1
    it works on gcc 4.9.2 with c++11. Your compiler most likely doesn't implement c++11 fully – bolov Jun 06 '15 at 08:48
  • 3
    @MarcoA. That's not just related, that's exactly it. A post-C++11 DR changed the rule, and C++11 compilers should implement the changed rule, but this was done in GCC 4.9. GCC 4.8 and earlier implement the original C++11 rule. You should post that as an answer. –  Jun 06 '15 at 08:50
  • probably duplicate of http://stackoverflow.com/questions/10509603/why-cant-i-initialize-a-reference-in-an-initializer-list-with-uniform-initializ – M.M Jun 06 '15 at 08:53
  • 1
    When reporting compilation problems you should mention the version number of the compiler, not the version of "code blocks". – StackedCrooked Jun 06 '15 at 16:57

2 Answers2

4

It works on C++14 and also works on C++11. You're very likely using an out-of-date compiler.

There's a fixed bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025) for your exact issue (cfr. DR 1288)

C++0x initialization syntax doesn't work for class members of reference type

Quoting from Jonathan Wakely

The original C++11 rules required a temporary to be created there and the reference member binds to that temporary.

Sources: Defect Report 1288

Marco A.
  • 43,032
  • 26
  • 132
  • 246
3

It works on both c++11 and c++14 on all these compilers:

gcc 4.9.2
gcc 5.1.0
clang 3.5.0

Most likely you are using an old version of gcc or another compiler that didn't implement yet this c++11 feature.

c++11 was implemented on stages by compilers. Each version added/improved/fix some c++11 features.

bolov
  • 72,283
  • 15
  • 145
  • 224