gpt4 book ai didi

c++ - 错误: anachronistic old-style base class initializer

转载 作者:行者123 更新时间:2023-12-03 07:39:15 25 4
gpt4 key购买 nike

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

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) {}

当然,因为我实际上没有初始化任何东西,所以它显然是损坏的代码。

但是,为什么它是基类初始化程序,为什么它是“过时的”而不是简单的错误呢?它曾经有效吗?什么时候?那是什么意思呢?

我在网上发现的 only rel ated references是人们意外地发现了一个错误,该错误是因为成员名称被意外地宏了,实际上导致了与上面相同的代码:
#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
};

这些人从来没有发现错误的含义,尽管他们后来至少发现了为什么程序被破坏了。

最佳答案

在第一个C++编译器1984-5版本CFront的文档中找到:

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().



如果您考虑一下,这是有道理的。大概添加了基类的显式命名,以支持多重继承。 (当时不支持成员初始化器列表-成员是无条件默认构造的-因此在多重继承之前,构造器可以在那里初始化一件事。)

感谢 http://www.softwarepreservation.org/projects/c_plus_plus/来归档文档。

……哇,我刚刚意识到“CFront”是文字游戏。

关于c++ - 错误: anachronistic old-style base class initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65140678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com