如果我在 Ubuntu 20.04 系统上的 cppyy v1.6.2 中运行以下测试脚本:
#!/usr/bin/python3
import cppyy
cppyy.cppdef("""
struct Test {
void test() const {
std::cout << std::is_same<int,double>::value << std::endl; // works
std::cout << std::is_same_v<int,double> << std::endl; // doesn't work
}
};
""");
tt = cppyy.gbl.Test()
tt.test()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
input_line_21:5:21: error: no member named 'is_same_v' in namespace 'std'
std::cout << std::is_same_v<int,double> << std::endl; // doesn't work
~~~~~^
input_line_21:5:34: error: expected '(' for function-style cast or type construction
std::cout << std::is_same_v<int,double> << std::endl; // doesn't work
~~~^
Traceback (most recent call last):
File "./test.py", line 18, in <module>
tt = cppyy.gbl.Test()
AttributeError: <namespace cppyy.gbl at 0x45baf10> has no attribute 'Test'. Full details:
type object '' has no attribute 'Test'
'Test' is not a known C++ class
'Test' is not a known C++ template
'Test' is not a known C++ enum
Run Code Online (Sandbox Code Playgroud)
因为我上面突出显示的那一行。其他一切都有效。
我知道那std::is_same_v是 C++17,但在 cppyy/cling 网页上我发现支持 C++17 的声明。到底是怎么回事?C++17 在 cppyy 中不起作用吗?这个可以配置吗?是否只有 C++17 的一个子集可用?
对于我的项目来说,C++17 至关重要......
为什么使用cppyy 1.6.2?最新版本是2.1.0。两者之间的一个很大的区别是后者的 Clang 底层有一个更新的版本(后者甚至还可以启用 c++2a)。也就是说,1.6.2 和 2.1.0 对我来说上述代码都没有问题,所以很可能是安装/构建问题。
首先,验证您的安装/构建中是否启用了 C++17。例如这样:
$ python
>>> import cppyy
>>> cppyy.gbl.gInterpreter.ProcessLine('__cplusplus')
Run Code Online (Sandbox Code Playgroud)
201703如果启用 C++17,结果应该等于或更高。
如果不是,请重新安装,例如使用 pip,假设您仍然需要 1.6.2(否则删除显式版本):
$ STDCXX=17 python -m pip install cppyy==1.6.2 --no-cache-dir --force-reinstall
Run Code Online (Sandbox Code Playgroud)
请注意,如果您的系统编译器(在构建 CPyCppyy 时使用)不支持 C++17,它仍会根据需要逐渐降低到 C++14 或 C++11,即使使用STDCXX=17.