constructors and references

Discussion in 'C++' started by copx, Apr 13, 2012.

  1. copx

    copx Guest

    This compiles:

    struct FileWriterI; struct FileWriter {
    FileWriterI& self;
    FileWriter(FileWriterI& i) : self(i) {}
    }

    This does not:

    struct FileWriterI; struct FileWriter {
    FileWriterI& self;
    FileWriter(FileWriterI& i) : self{i} {}
    }

    Why? Why can't I initialize references using {} in constructors? I use
    GCC 4.7.0, c++11 mode.
     
    copx, Apr 13, 2012
    #1
    1. Advertising

  2. On 4/13/2012 1:00 PM, copx wrote:
    > This compiles:
    >
    > struct FileWriterI; struct FileWriter {
    > FileWriterI& self;
    > FileWriter(FileWriterI& i) : self(i) {}
    > }
    >
    > This does not:
    >
    > struct FileWriterI; struct FileWriter {
    > FileWriterI& self;
    > FileWriter(FileWriterI& i) : self{i} {}
    > }
    >
    > Why? Why can't I initialize references using {} in constructors? I use
    > GCC 4.7.0, c++11 mode.


    Maybe because the braces aren't part of the C++ syntax for the member
    initializers... Just a wild guess... Brackets won't work, either. Nor
    will angle brackets... Only parentheses.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 14, 2012
    #2
    1. Advertising

  3. copx

    copx Guest

    On 14.04.2012 02:58, Victor Bazarov wrote:
    > Maybe because the braces aren't part of the C++ syntax for the member
    > initializers... Just a wild guess...


    You guessed wrong:
    http://en.wikipedia.org/wiki/C++11#Uniform_initialization

    ...and my compiler accepts such initialization with -std=c++11 -Wall
    -Wextra -pedantic for all types.. except references. The question
    remains: why?!

    As I said, all other types are fine, including pointers!
     
    copx, Apr 14, 2012
    #3
  4. copx

    copx Guest

    On 13.04.2012 19:00, copx wrote:
    > This compiles:
    >
    > struct FileWriterI; struct FileWriter {
    > FileWriterI& self;
    > FileWriter(FileWriterI& i) : self(i) {}
    > }
    >
    > This does not:
    >
    > struct FileWriterI; struct FileWriter {
    > FileWriterI& self;
    > FileWriter(FileWriterI& i) : self{i} {}
    > }
    >
    > Why? Why can't I initialize references using {} in constructors? I use
    > GCC 4.7.0, c++11 mode.
    >


    Finally found the answer:

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

    It's a bug in the C++11 standard itself!
     
    copx, Apr 14, 2012
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,306
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    591
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    610
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    400
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    472
Loading...

Share This Page