簡體   English   中英

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 對我來說上面的代碼沒有問題,所以很可能是安裝/構建問題。

首先,驗證您的安裝/構建中是否啟用了 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,它仍然會根據需要STDCXX=17降低到 C++14 或 C++11,即使使用STDCXX=17

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM