(据我所知,使用的编译器是带有 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) 我在使用这段代码时遇到了错误:
class Box {
public:
Box (int);
};
Box::Box (int a) {
//sample code
}
int main() {
class Anything {
Box box (5); // error: expected identifier before numberic constant
// error: expected ',' or '...' before numeric constant
};
}
Run Code Online (Sandbox Code Playgroud)
该错误出现在我在 Anything 类下填写的五项上。如果我只是写,这个问题就会消失。
Box box (5);
Run Code Online (Sandbox Code Playgroud)
没有周围的 Anything 类。
任何帮助,将不胜感激。