错误:不合时代的旧式基类初始化器 [英] error: anachronistic old-style base class initializer

查看:475
本文介绍了错误:不合时代的旧式基类初始化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在C ++ 98,C ++ 11和C ++ 14模式下尝试过的所有版本的GCC上产生后续编译错误:

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

// main.cpp:在构造函数'T :: T(void *)'中:
// main.cpp:3:18:错误:不合时代的旧式基类初始值设定项[-fpermissive]
// T(void * x):(x){}
// ^
// main.cpp:3:16:error:unnamed initializer for'T ',它没有基类
// T(void * x):(x){}

当然,这显然是破解的代码,因为我实际上并没有初始化任何东西。

但为什么它是一个基类初始化程序,为什么它不合时宜,而不是简单的错误?曾经有效吗?什么时候?这是什么意思? only rel

  #define bar 
// ^有些图书馆可以做到这一点

struct t
{
T(int x)
:bar(x)//实际上只是`:(x)`
{}

int酒吧; //会导致自己的错误
};

这些人从未发现错误的含义,虽然他们后来至少发现了他们的程序为什么是破解。

在1984年5月发布的CFront第一版C ++编译器文档中发现: b
$ b


构造函数可以这样写:

  vec (hb-lb + 1)
{
if(hb-lb <0)hb * lb
low = lb;
high = hb;

$ / code>

构造:(hb-lb + 1)用于指定参数列表需要的基类构造函数vector()。


如果您仔细考虑,据推测,基类的显式命名是为了支持多重继承而添加的。

归功于 http://www.softwarepreservation.org/projects/c_plus_plus/ 用于归档文件。

...哇,我刚才 意识到CFront是一个文字游戏。

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.

解决方案

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.

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.

这篇关于错误:不合时代的旧式基类初始化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆