sfinae

    1熱度

    1回答

    人們經常將SFINAE用於函數。例如: #include <type_traits> #include <iostream> template<int N>//version for N!=4 typename std::enable_if<N==4, int>::type val(){ return N; } template<int N>//version for N=

    0熱度

    1回答

    我使用SFINEA訂閱到ROS通用主題,偵聽每一個主題,並使用SFINEA如果存在返回header.stamp時間。這反序列化的速度更快。唯一的問題是我有一個問題讓我的訂戶設置。我不斷收到以下編譯錯誤: CMakeFiles/performance_tracker.dir/src/performance_tracker.cpp.o: In function PerformanceTracker:

    4熱度

    1回答

    下面是我用真實代碼命中的問題的簡化版本。 短版:只要看看在gcc.godbolt.org /長版本的代碼和錯誤:閱讀;) 假設我要一類模板參數setting和int func(int)如方法: 時setting爲false,func返回參數 時setting是true,func雙打其參數 要做到這一點,最簡單的方法是特類模板: template<bool setting> struct A {

    2熱度

    2回答

    我實現了std::experimental::is_detected基於on this article on cppreference.com(部分代碼在+ working repro之下)。 它適用於G ++和Clang ++,但會導致MSVC出現奇怪的錯誤行爲:is_detected似乎總是bool_constant<true>! 在這裏你可以正確的結果使用gcc 5.x看到: @ideone

    0熱度

    1回答

    我想實現這樣的事情: template<typename Bar> class foo { public: template<class = std::enable_if_t<std::is_constructible<Bar, int>::value>> foo(int x) : m_bar(x) { } private: Bar m

    0熱度

    1回答

    當我嘗試實例化具有部分專業化的模板化函子時,出現歧義錯誤。下面的Foo和Bar是相同的,只是Foo的enable_if_t被替換爲Bar中的void。 #include <iostream> #include <vector> using namespace std; template<class T, class Enable = void> struct Foo; te

    1熱度

    2回答

    std::underlying_type與非枚舉類型一起使用時會調用未定義的行爲。 但未定義的行爲出現在哪裏? 在此代碼: template<typename E> constexpr std::enable_if_t<std::is_enum<E>::value, std::underlying_type_t<E>> IntEnum(E e) { return static_cas

    7熱度

    2回答

    我想打一個構造一類,使用任何整數類型,但符號和無符號區分。我不希望這是班級本身的模板。以下不起作用。 Visual Studio只是說沒有參數可以匹配。 class Thing{ public: template<typename Integral> Thing( typename std::enable_if< std::is_integral<In

    1熱度

    3回答

    我想將屬性添加到C++類中,該類可以有幾種類型(例如float,int,bool)。根據它們的值類型,應該在界面中顯示不同的控件。 要做到這一點,我創建使用SFINAE爲type()功能的簡單屬性類: #include <iostream> #include <type_traits> template <class T> class Property { public: e

    1熱度

    2回答

    比方說,我們有3種類型(S1,S2和S3): //type 1 (good type) template<typename T1, typename T2, typename T3> struct S1{}; //type 2 (good type) template<typename T1, typename T2, typename T3, int T4> struct S2{};