sfinae

    4熱度

    3回答

    我目前正試圖實現一個toString功能.toString()或std::to_string()這取決於可用於推導型 到目前爲止,我有這方面的工作片段的呼叫: #include <iostream> #include <string> template <class T> auto toString(const T& obj) -> decltype(obj.toString(

    2熱度

    1回答

    我試圖實現一種機制來檢測提供的類是否包含一些靜態方法。這是很簡單的代碼,但我不明白爲什麼decltype()如預期EnableIfHasFooMethod類的專業化不起作用: #include <iostream> struct A { static int Foo() { return 0; } }; template <class T, class = void> str

    1熱度

    1回答

    結果我有constexpr函數計算佔位符https://godbolt.org/g/JcxSiu的數量, e.g: 「Hello %1」 返回1和 「Hello %1, time is %2」 返回2。 然後,我想作出一個函數,如果參數的數量不等於佔位符的數量,它不會編譯。 template <typename... Args> inline std::string make(const cha

    1熱度

    2回答

    我試圖創建一個函數重載因此它只能結合(工程)成員函數。我接過來一看在std::mem_fn http://en.cppreference.com/w/cpp/utility/functional/mem_fn template <class Ret, class T> /* unspecified */ mem_fn (Ret T::* pm); 函數簽名所以我構建我的參數,例如 templ

    1熱度

    2回答

    我想從std::ifstream實現一些讀取功能。 它需要分開pod類型和其他類型。 (目前std::string) template <typename T, typename = std::enable_if<std::is_pod<T>::value>::type> T read(std::ifstream& fin); template <> std::string read<st

    0熱度

    2回答

    任何人都可以解釋爲什麼下面的代碼在Visual Studio 2015 C++中出現錯誤「錯誤C2259:'PropertyValue':無法實例化抽象類」? 編譯器無法識別派生類PropertyValue中的條件指定函數ConvertToDevice()是否具有相同的簽名? 非常感謝, 約翰 #include <type_traits> #include <typeinfo> class

    0熱度

    2回答

    我有一組其接收的索引(在示例的int)模板函數和返回給定的類型的值,我使用SFINAE從算術類型分開std::string: // 1 template <typename T> typename std::enable_if<std::is_arithmetic<T>::value, T>::type t(int) { ... } // 2 template <typename T>

    2熱度

    1回答

    我想專精getVector成員函數,我正在嘗試使用SFINAE。但它只有在Dim爲3或更大時纔有效。 template <size_t Dim> class Mat { ... template <size_t VDim, typename enable_if<(Dim > 1 && VDim == 0)>::type* = nullptr> void getV

    0熱度

    1回答

    的例子考慮下面的代碼: // get the return type of == for T1 and T2 template<typename T1, typename T2> using equals_op_type = decltype(std::declval<T1>() == std::declval<T2>()); template <class Container1, cla

    5熱度

    2回答

    我想std :: enable_if第一次和掙扎。 任何指導將不勝感激。 當作玩具例子,這裏是一個簡單的靜態矢量類,爲此我要定義一個拷貝構造函數,但行爲取決於載體的相對大小: 只是將數據複製到更小或相同大小的矢量 複製數據到一個較大的載體中,然後墊用零 其餘部分,以便所述載體類是: template <size_t _Size> class Vector { double _data