cppyy 和 std::is_same_v (C++17)

cppyy and std::is_same_v (C++17)

如果我 运行 在我的 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()

我收到以下错误消息:

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

因为我在上面突出显示的行。其他一切正常。

我知道 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 对我来说上面的代码都没有问题,所以很可能是 installation/build 问题。

首先,验证您的 install/build 中是否启用了 C++17。例如。像这样:

$ python
>>> import cppyy
>>> cppyy.gbl.gInterpreter.ProcessLine('__cplusplus')

如果启用了 C++17,结果应该是 201703 或更高。

如果不是,请重新安装,例如使用 pip,假设您仍然需要 1.6.2(否则删除显式版本):

$ STDCXX=17 python -m pip install cppyy==1.6.2 --no-cache-dir --force-reinstall

请注意,如果您的系统编译器(在构建 CPyCppyy 时使用)不支持 C++17,它仍会根据需要逐步降低到 C++14 或 C++11,即使 STDCXX=17.