Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The following code produces the subsequent compilation error on all versions of GCC that I've tried, in C++98, C++11 and C++14 modes:

struct T
{
    T(void* x) : (x) {}
};

// main.cpp: In constructor 'T::T(void*)':
// main.cpp:3:18: error: anachronistic old-style base class initializer [-fpermissive]
//      T(void* x) : (x) {}
//                   ^
// main.cpp:3:16: error: unnamed initializer for 'T', which has no base classes
//      T(void* x) : (x) {}

Sure, it's clearly broken code because I'm not actually initialising anything.

But why is it a base-class initialiser and why is it "anachronistic", rather than simply wrong? Was it once valid? When? And what did it mean?


The only related references I've found to this on the web have been people coming across the error when a member name was accidentally macro'd out, effectively resulting in the same code as above:

#define bar
// ^ some library could have done this

struct T
{
    T(int x)
        : bar(x)   // effectively just `: (x)`
    {}

    int bar;       // will cause its own error
};

Those people never did find out what the error meant, although they later at least discovered why their program was broken.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

Found in the documentation for the 1984-5 release of CFront, the first C++ compiler:

The constructor can be written like this:

  vec.vec(int lb, int hb) : (hb-lb+1)
  {
      if (hb-lb<0) hb = lb;
      low = lb;
      high = hb; 
  }

The construct : (hb-lb+1) is used to specify the argument list needed for the base class constructor vector().

Makes sense, if you think about it. Presumably the explicit naming of the base class was added to support multiple inheritance. (At the time there was no support for member initializer lists -- members were unconditionally default-constructed -- so before multiple inheritance there was exactly one thing a constructor could initialize there.)

Credit to http://www.softwarepreservation.org/projects/c_plus_plus/ for archiving the documents.

...and wow, I just now realized that "CFront" was a play on words.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...