enable-if

    0熱度

    1回答

    我正在玩C++,嘗試使用模板,枚舉類等。在我的涉獵中,我遇到了一個問題,我不確定是否有解決問題的好方法。我有兩種不同類型的「內存」:mem1和mem2。這兩者都有關於如何訪問它們的特殊策略,但方法相同:create,read,& write。現在,取決於內存的訪問類型,例如,如果訪問類型是隻讀的並且其調用write(),我希望編譯器給我一個錯誤。這是我到目前爲止有: enum class mem1

    1熱度

    2回答

    我對模板元編程相當陌生,並且一直在研究一些概念 - 但是,我遇到了這個特定片段,我一直被困住了。 template<class TAG, typename... DATATYPES> struct Message { Message (typename std::enable_if<sizeof...(DATATYPES) >= 1>) { } ... (var

    1熱度

    1回答

    假設我有一個純粹的抽象基類。類模板實現了這個接口,並且是專用的。現在,我的問題是這個專業化應該能夠處理專業化的子類。所以,我嘗試了enable_if,但是這個子類最終變成了抽象的...我怎樣才能解決這個問題? 例如: // This example doesn't work because a subclass of A does not satisfy the // specializatio

    8熱度

    5回答

    給定一個數組a,我想countof(a)產生的數組中的元素數量作爲編譯時常量。如果我有一個指針p,我想countof(p)不能編譯。這看起來應該是(1)簡單明瞭,(2)通常包含在SO中,但是(1)我無法讓它工作,以及(2)搜索SO沒有發現任何東西。 這是我的嘗試。 #include <cstddef> #include <type_traits> template<typename T, s

    1熱度

    1回答

    #include <boost/type_traits.hpp> #include <boost/utility.hpp> using namespace boost; struct Serializable{}; struct Derived : Serializable {}; class Serializer { public: template <typen

    15熱度

    3回答

    香草薩特的回到基礎! CppCon的現代C++演示文稿討論了傳遞參數的不同選項,並比較了它們的性能與易於編寫/教學。 「高級」選項(在所有情況下提供最佳的性能測試,但太難對於大多數開發者編寫)是完美轉發,給定(PDF, pg. 28)的例子: class employee { std::string name_; public: template <class String

    0熱度

    2回答

    是否有可能以某種方式使函數(C++ 11),它將接受不同的參數取決於第一個? 比方說,我需要以下行爲: enum TypeFill{Solid, Gradient1, Gradient2}; void fill(const TypeFill t, Type1 param1 = NULL, Type2 param2 = NULL){ if (t == Solid){ fil

    1熱度

    2回答

    我想使用boost :: enable_if打開/關閉類模板中的某些功能,但始終得到編譯錯誤錯誤:無類型名爲「type」的struct boost :: enable_if。 我的片段: #include <iostream> #include <tr1/type_traits> #include <boost/utility.hpp> namespace std { using

    14熱度

    3回答

    場景: 我有多種類型,可以分類爲序列容器。 所有的序列容器都是數據結構,但不是每個數據結構都是序列容器。 下面是代碼中所示的例子。這個例子中唯一的「重要類型」是Array_T。它分爲兩類:它是一個序列容器,並且由於所有序列容器都是數據結構,所以它又是一個數據結構。 //A sequence container type class Array_T{}; //A type trait for

    2熱度

    2回答

    我試圖專注一個成員函數模板兩種不同類型的類如下: #include <iostream> #include <boost/utility/enable_if.hpp> struct Wibble { static const bool CAN_WIBBLE = true; }; struct Wobble { static const bool CAN_WIBB