sfinae

    0熱度

    1回答

    我正在使用SFINAE構建一個基本的序列化庫。 比方說,我有一個類實現了一個通用的process方法,該方法讀取任何類型(允許用戶擴展)並對其進行序列化。我正在使用std::enable_if將此方法專用於不同的模板參數類型。下面是一個例子: class OutputSerializer { public: OutputSerializer(std::ostream& str) :

    0熱度

    4回答

    我想使用static_assert強制失敗。如果您嘗試以特定方式實例化特定的模板化函數,我想生成編譯器錯誤。我可以讓它工作,但它真的很難看。有沒有更簡單的方法來做到這一點? 這是我第一次嘗試。這根本不起作用。即使沒有人試圖使用這個函數,它總是會產生一個錯誤。 template< class T > void marshal(std::string name, T *value) {

    9熱度

    2回答

    我想檢查某個模板專業化是否存在,其中一般情況下沒有定義。 考慮: template <typename T> struct A; // general definition not defined template <> struct A<int> {}; // specialization defined for int 我想這樣定義一個結構: template <typename T>

    4熱度

    3回答

    我想擁有一個接受不同類型參數的泛型函數(或方法)。如果提供的類型具有'one'方法,則該函數應該使用它。如果它有'兩個'方法,該函數應該使用它。 這裏是無效的代碼: #include <iostream> template<typename Type> void func(Type t) { t.one(); } template<typename Type> void fu

    0熱度

    1回答

    忽略返回類型,有這樣一段代碼: template <typename T, typename R> struct is_dereferenceable_exact { typedef char yes; typedef struct { char dummy[2]; } no; template <typename U, R (U::*)() const>

    2熱度

    2回答

    如何在不使用void_t的情況下實現C++ detection idiom?換句話說,我可以僅使用C++ 03特性來實現C++ 17 std::is_detected等嗎? UPD根據定義,檢測習慣用法需要C++ 11。在我的問題中,我的意思是我想實現is_detected而不是void_t。我的問題在於:別名模板中未使用的參數不能保證確保SFINAE並可以被忽略,VS VS 2013有這個缺陷;

    17熱度

    3回答

    使用C++ 11的enable_if我想定義幾個專門的函數實現(基於參數的類型,比如說)以及默認實現。什麼是定義它的正確方法? 以下示例因爲調用「generic」實現而無法按預期工作,無論類型爲T。在我的小例子, #include <iostream> template<typename T, typename Enable = void> void dummy(T t) { s

    5熱度

    3回答

    我想創建部分專用模板,如果傳遞了一個std::unique_ptr template <typename T, typename = void> struct Foo; // A template <typename T> struct Foo<std::unique_ptr<T>, typename std::enable_if<std::is_class<T>::value>::ty

    3熱度

    2回答

    Demo A :: foo的類聲明。 struct A { template <typename T> void foo(T a); }; A :: foo現在被sfinae分割。 template <typename T> typename std::enable_if<(sizeof(T) > 4), void>::type A::foo(T a) { s

    2熱度

    1回答

    如果我取消註釋包含foo<double>()的行,因爲B<double>取決於A<double>,這是一個不完整的類型,所以下面的程序不能編譯。 #include <iostream> using namespace std; template <class T> struct A; // forward declaration (incomplete) template <> stru