enable-if

    1熱度

    2回答

    下面的代碼適用於gcc 4.7。這個想法是我有這些泛型函數,這些泛型函數在序列,指針,圖簇,對,用戶定義的類型以及其他類型上工作。如果其中一個函數是爲一個類型定義的,那麼全部都應該是。我遇到的問題是確定如何專門化他們。我決定定義一個專門用於每種類型的模板類,實現每個函數,然後定義一個自由函數,將其轉發給課堂實現。 #include <utility> #include <vector> #in

    8熱度

    1回答

    我正在寫一個簡單的向量類,我想有一些成員函數只在某些長度的向量中可用(cross product for例如3元素矢量)。我偶然發現std :: enable_if,它看起來可能能夠做我想做的事,但我似乎無法正常工作。 #include <iostream> #include <type_traits> template<typename T, unsigned int L> class

    -1熱度

    1回答

    我對std :: enable_if很陌生,想知道如何使用它。 我有一個模板類: template<int a, int b> class foo { int c; } 我只希望模板有成員c當 a = 5. 我該怎麼做,使用的std :: enable_if? 這是一個正確的情況下使用std :: enable_if?

    2熱度

    1回答

    下面的代碼失敗,英特爾C++ 2013年 #include <type_traits> #include <iostream> template < typename T, typename std::enable_if<std::is_integral<T>::value>::type > void myfunc(T a) { std::cout <<

    8熱度

    1回答

    如果我有一個封裝了標準容器的模板,看來我可以合理輕易委託initializer_list的構造函數: template<typename T> struct holder { T t_; holder() : t_() {} holder(std::initializer_list<typename T::value_type> values)

    0熱度

    3回答

    我有一個模板化的類,我想只在類型爲double時啓用某個構造函數。這段代碼有什麼問題? template<typename T> class B: public A<T> { public: B(int arg1=0, typename boost::enable_if_c<boost::is_same<T, double>::value>=0); } int main(in

    17熱度

    2回答

    考慮下面的代碼: template<bool AddMembers> class MyClass { public: void myFunction(); template<class = typename std::enable_if<AddMembers>::type> void addedFunction(); protected:

    0熱度

    1回答

    我有代碼,編譯和運行如預期在gcc,並不編譯在MSVC 2012 RC,我不能解釋爲什麼,所以它的錯誤MSVC,或我的代碼是不正確的? #include <boost/mpl/vector.hpp> #include <boost/mpl/front.hpp> #include <boost/mpl/is_sequence.hpp> #include <boost/mpl/size.hpp>

    6熱度

    1回答

    我想寫一個MyClass的構造函數,它帶有一個參數,並且我希望只在參數爲pointer或iterator(iterator_traits)時才編譯它。如何實現這一目標?

    23熱度

    5回答

    爲什麼這不與gcc48和clang32編譯? #include <type_traits> template <int N> struct S { template<class T> typename std::enable_if<N==1, int>::type f(T t) {return 1;}; template<class T>