(据我所知,使用的编译器是带有 c++17 的 gcc(在 Visual Studio 中很难找到))
#include <iostream>
using namespace std;
void increment( int& v )
{
++v;
}
int constexpr f()
{
int v = 0;
increment( v );
return v;
}
int main( )
{
cout << f( ) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了编译错误:
constexpr 函数 'f' 不能产生常量表达式。
据我了解,这是因为该函数increment不是 constexpr。让我困惑的是以下代码编译得很好:
#include <iostream>
using namespace std;
void increment( int& v )
{
++v;
}
int constexpr f()
{
int v = 0;
for( int i = 0; …Run Code Online (Sandbox Code Playgroud) n3797说:
§7.1.6.4/14:
使用占位符类型的返回类型声明的函数不应是虚拟的(10.3).
因此,以下程序格式错误:
struct s
{
virtual auto foo()
{
}
};
Run Code Online (Sandbox Code Playgroud)
虚拟
允许对虚函数进行返回类型推导是可能的,但这会使重写检查和vtable布局复杂化,因此似乎最好禁止这种情况.
任何人都可以提供进一步的理由或给出一个与上述引用一致的好(代码)示例吗?