[英]Is there a way to get the type of a dereferenced value from a dereference-able type?
我在玩 C++ 概念,遇到了一個有趣的問題。 我有以下兩個自定義概念: 顧名思義,第一個用於確定給定類型是否可以取消引用,而另一個用於檢查類型是否支持 output 運算符。 我還有一個名為println的 function 模板,如下所示: 當且僅當類型T是可取消引用並且取消引用值的類型是可打印 ...
[英]Is there a way to get the type of a dereferenced value from a dereference-able type?
我在玩 C++ 概念,遇到了一個有趣的問題。 我有以下兩個自定義概念: 顧名思義,第一個用於確定給定類型是否可以取消引用,而另一個用於檢查類型是否支持 output 運算符。 我還有一個名為println的 function 模板,如下所示: 當且僅當類型T是可取消引用並且取消引用值的類型是可打印 ...
[英]Concept is satisfied by a type that would seemingly produce an invalid expression
在下面的代碼中, can_foo概念測試是否可以在類型的實例上調用foo()成員 function。 我將使用它來測試兩個模板的實例: base有條件地啟用foo成員 function, derived覆蓋foo以調用其 base 的實現:template <typename T> co ...
[英]How to declare the signature of generic higher order functions with concepts in c++ 20?
以下代碼聲明 function Foo返回的 function 必須是 model 和 function,它接受 1 個 int 並返回一個 int。#include <functional> #include <concepts> std::convertible_to ...
[英]How to do partial template class specialization with concept and requires?
用概念寫模板 function 覆蓋很容易,但我不知道如何寫模板 class 偏特化:( 這也不起作用:( ...
[英]How can I enable/disable a function depending on the size of an array?
我對 TMP 還是很陌生,所以如果這是一個措辭不佳的問題,請原諒我。 我試圖制作一個非常通用的數學向量 class 來存儲任意數量的組件,但默認為 3 並使用 float 作為它的基本表示。 因此,如果您默認構建這些向量之一,它將包含(0.0f,0.0f,0.0f) 這些值本身存儲在std::ar ...
[英]How do I write a C++ requires expression that looks for specific member functions/static functions with arguments?
我如何編寫C++ requires 表達式來查找具有特定參數類型的特定成員函數或 static 函數? 例如:struct ConcretePrintable { static std::string StaticDescription() { return "static descrip ...
[英]Disabling function that uses concept constrain if concept is not met
如何編譯以下代碼? 我正在嘗試檢查類型中是否存在 BigStruct,如果存在,則啟用f 。 我得到的錯誤: 這是 x86-64 clang trunk 的神栓鏈接。 ...
[英]Pass initializer lists and ranges seamlessly to a function parameter
我正在嘗試創建一個 function 來初始化一個內部std::set<std::string> ,我想公開一個 API 允許任何類型的范圍作為輸入,包括初始化列表。 所以我的目標是:// API definition class Toto { public: void set_ ...
[英]Can a concept be declared deprecated?
我剛剛遇到一個案例,我想聲明一個 C++ 20 concept棄用。 但是,我的編譯器 (Apple Clang 14) 似乎不接受向concept添加[[deprecated]]屬性。 我的嘗試看起來像 是否完全不支持棄用概念(還),我是否選擇了錯誤的語法或者這是我的編譯器的缺陷? ...
[英]Copy and move constructors of an input iterator
根據std::input_iterator概念指定的要求(和高級迭代器似乎是相同的),它們必須提供復制和移動構造函數: 他們可以違約嗎? 典型的實現包含一個指向某些數據的私有成員: 在迭代器中是淺拷貝/移動所需的行為嗎? 我的意思是,復制/移動指針本身,還是必須實現一個顯式的深拷貝? ...
[英]template parameter constraints make more general type test fail
#include <concepts> #include <tuple> template<std::integral... Is> using arithmetic_tuple = std::tuple<Is...>; template<t ...
[英]Syntax for a referenceable concept
我正在嘗試模仿can-referece exposition only 概念,它可以在新的 C++20 迭代器概念中找到。 從cppreferece中提取: 因此,我嘗試實現這些概念,如下所示: 使用Clang 15.0.0編譯得很好。 但是,如果我嘗試刪除冗長的內容,則直接定義類型T是可引用的,而 ...
[英]pack expansion used as argument for non-pack parameter of concept
Clang 和 gcc 在第 2 行抱怨“包擴展用作概念的非包參數的參數”。 MSVC 編譯。 代碼格式是否正確? 這是一個更具體的例子,說明了為什么這個問題很重要。 假設我們要定義一個帶有參數包的概念,稱為single_integral_or_all_floating_point 。 如果包 ...
[英]Why do different compilers behave differently with these requires expressions?
為什么在 C++ 20 中使用 requires 表達式的“Oneable”概念的某些實現不能在某些編譯器上編譯?// `Oneable` implemented using `requires` with a simple assignment expression // clang, gcc ...
[英]C++20 how to have concept require a free function that is defined later
假設我有一個概念MyConcept描述了通用算法algo的要求。 在下面的示例代碼中,唯一的要求是一個免費的 function my_free_function ,它采用MyConcept類型並返回相同的類型,並且有三種類型可以滿足要求。 我的挑戰是,為了獲得以下調用以針對 NativeU64 和 ...
[英]C++20 concept matching string literal
在 C++20 中,可以編寫一個只接受字符串文字的包裝器 class。 來自 OP 自己的回答https://stackoverflow.com/a/74922953 是不是也可以寫一個只匹配字符串字面量的概念? ...
[英]a concept to check if at least one of the type in a list of types can be constructed from a given type
我有一個類型列表TypeList 。 我目前正在使用元組來保存這些類型。 我想要的是以下內容: 給定一個新類型X - 檢查是否至少有一個來自TypeList的類型可以從X構造。 我也想使用現有概念std::constructible_from 我從以下概念定義開始: 但我收到以下錯誤: 看起來,需要 ...
[英]Order of definition of constrainsts and concepts c++20
概念或約束的權利 position 在哪里? 以下代碼編譯: 但如果我將 function f(int)的 position 更改為: 這不會在 gcc 11.3.0 中編譯。 我收到以下錯誤: ...
[英]What is the rationale to disallow type traits in type-constraints in addition to concepts?
語言規范在引入概念時,隱含地不允許在類型約束上下文中使用類型特征來代替概念: 我依稀記得在規范過程中,如果不引入新的concept關鍵字和相應的定義概念的機制,人們就無法使概念的概念發揮作用。 但是,在A的定義中,構造template <bool-expression ID>從來都不合 ...
[英]Check if type is specialization of template for both type and non-type template parameters
我一直在嘗試執行檢查以查看類型是否是特定模板的特化。 我在網上看到了很多答案,但大多數只適用於類型參數。 我發現這個 SO answer確實涵蓋了非類型參數。 然而,這是不可取的,因為它將它們包裝在一個結構中,這使得為它創建一個概念變得非常困難。 它還需要顯式指定非類型模板參數。 根據我在網上看到的 ...