[英]force template specialization with an incomplete (partially specified) templated type
當我嘗試使用不完整(部分指定)的模板化類型強制進行模板專業化時,我遇到了一些問題。 讓我們考慮這個過於簡單的示例:我有一個模板 function 聲明,我在相應的 C++ 文件中為某些常見類型提供了強制規范。 在.h文件中: 在.cpp文件中: 例如,我想對glm 向量(來自 OpenGL 數學庫) ...
[英]force template specialization with an incomplete (partially specified) templated type
當我嘗試使用不完整(部分指定)的模板化類型強制進行模板專業化時,我遇到了一些問題。 讓我們考慮這個過於簡單的示例:我有一個模板 function 聲明,我在相應的 C++ 文件中為某些常見類型提供了強制規范。 在.h文件中: 在.cpp文件中: 例如,我想對glm 向量(來自 OpenGL 數學庫) ...
[英]How to define a method only when class generic type satisfies a test?
我正在嘗試執行以下操作:class Event<TPayload>() { fun subscribe(handler: (payload: TPayload) -> Unit) { ... } fun subscribe(handler: () -> Unit ...
[英]Template specialization for the base template type for future derived types
我有一個 class 用作某些原語或自定義類型的包裝器。 我想為自定義模板類型編寫明確的專業化。 我重現問題的代碼: A<DerrType>使用默認的 function,不是專門的。 我怎樣才能使這些類的專業化? 我會有很多像DerrType這樣的類型,並且我想為所有這些類型做出共同的 ...
[英]Function template overload with constraints
我有以下代碼:// converters.h #pragma once #include <iostream> #include <type_traits> template <typename TTo, typename TFrom> static TTo ...
[英]Exact rules for matching variadic template template parameters in partial template specialization
在為另一個問題創建此答案時,我遇到了以下問題。 考慮這個程序( godbolt ): 輸出(也是預期的)如下(使用 clang 14.0.0 以及 gcc 12.1): 但是,如果模板模板的參數列表中的三個點被刪除,像這樣(整個程序在godbolt上運行): ,那么clang和gcc的 ...
[英]Adding new constructors to a specialised template class
我有一個類用一些方法定義一個固定長度的數組n 。 假設我想向array_container<3>添加一個構造函數,類似於: 我知道有兩種方法可以做到這一點: 一種是復制泛型類的整個代碼,將n替換為 3,並添加我的構造函數: 這可以正常工作,但缺點是需要從通用庫中復制所有代碼 ...
[英]C++ variadic function template with multiple typelists
Compiler Explorer Demonstration顯示了我發現的有效內容,以及顯示我想要但不起作用的注釋部分。 我是 C++ 的新手,我正在嘗試用 C++20 編寫一個 sqlite3 接口,它對查詢參數和返回的列類型進行類型檢查。 我已經被困了好幾天了,讀了這么多。 我確信其中一個有 ...
[英]Partial specialization of typedef
我想要一個 typedef 或 class 的類型。 因此,像這樣 顯然,這不會編譯。 為了克服這個問題,我“發明”了以下結構,在我看來這似乎相當復雜:template< typename T > class A { }; template< typename T, bool ...
[英]Template function specialization for specific template ( not type )
我有一些模板化的 class 類型,例如 A、B、C,如下所示: 現在我想要一個 function 通常接受任何類型,例如: 現在我想專門化 function 以僅接受給定模板類之一,例如: 這是不允許的,因為它只是部分專業化。 好的。 我認為concept可能會有所幫助,但感覺我想得太復雜了。 ...
[英]Can template partial specialization narrow the argument type in C++?
在下一個程序中,結構模板A<int>有一個特化A<char> : Clang 接受整個程序。 GCC 接受特化定義,但在A<1>{}中忽略它。 MSVC 抱怨這種專業化: 演示: https://gcc.godbolt.org/z/Ef95jv5E5 哪個 ...
[英]Is this partial class specialization buggy or is it a bug in clang or gcc?
GCC可以編譯這個。 但是 Clang不能。 錯誤 output: ...
[英]Content of class method determined by the template value
通過使用 c++ 14 或 c++11,我們是否有一種優雅的方法來執行以下任務? 成員數量和操作類型由模板輸入值“count”確定 template<int count> class show{ public: run(){ ...
[英]C++ partial specialization of template without code duplication
我有一個帶有 3 個模板參數的 class 定義。 我想用 2 個參數的特定組合創建這個 class 的特化,讓第三個自由。 如何在不重復代碼的情況下做到這一點? ...
[英]How to define a specialized class method outside of class body in C++?
我有一個模板類A<T>及其對整數參數的專業化。 並且類及其特化都聲明了方法foo() ,我想在類主體之外定義它: GCC 接受此代碼。 Clang 打印錯誤https://gcc.godbolt.org/z/hYfYGPfMh : MSVC 會在兩個方法定義上打印錯誤: ...
[英]Partial specialization C++
再會! 我正在嘗試構建一個模板 class ,它的幾個部分專業化如下:#include <iostream> struct MainT {}; struct Foo {}; template <int dim> struct Tensor {}; template ...
[英]How to check if a template argument is of a particular templated type (multiple type parameters)
我昨天問了一個問題( 如何確定一個類型是否是任何類型的模板化類型? ),關於當該參數是任何類型的模板化 class 時如何檢查特定模板參數。 解決方案是這樣的:template <typename T> struct Animal{}; template <typename T ...
[英]Partial specialization difference for C++14 versus C++17?
編碼: 在 C++14 級別編譯時 gcc-10.2、clang-linux-11.0、VC++-14.2 和 VC++-14.2 預覽版編譯/鏈接沒有問題。 但是在C++17級別編譯時,雖然gcc-10.2仍然編譯代碼沒有任何問題,但是clang、VC++和VC++預覽都報錯。 對於 clang ...
[英]Partial specialization and const conundrum in C++11
一個 class 模板: template <class X, class Y > class MyTemplate; 我想部分專門化這個 class 以便當 X 是 const 類型時,Y 是int並且如果我創建 object 則選擇部分專門化代碼,例如: MyTemplate< ...
[英]c++ template member function with partial spezialisation outside class
此代碼使用對 std::vector 和其他類型不同的模板成員。 到目前為止,它工作正常。 但是,在 class 之外為 std::vector 定義 get 方法的正確語法是什么? 我知道使用特征和其他幫助器來使這個工作,但如果有一個有效的語法,我想要這個簡單的解決方案。 這似乎不起作用 ...
[英]Failure on partial template specialization with gcc 9.3, was working before
我檢查了This stackoverflow post ,但我仍然無法理解。 我有這段代碼(是的,我知道<functional>實現了同樣的事情)有效(如果 memory 對我有用,它是 gcc6),現在失敗了 gcc9.3.0 鑒於這些 class 定義 和這些定義 我有這些子類 現在 ...