1

クラステンプレートの一部の関数をオン/オフするために boost::enable_if を使用しようとしていますが、常にコンパイルエラーerror: no type named "type" in struct boost::enable_if が発生します。

私のスニペット:

#include <iostream>
#include <tr1/type_traits>
#include <boost/utility.hpp>

namespace std {
    using namespace tr1;
}

template <typename T1>
struct C {
    template< typename T2 >
    void test( T2&, typename boost::enable_if<
    std::is_const< T1 >, T1 >::type* = 0 ) {

        std::cout << "const" << std::endl;
    }

    template< typename T2 >
    void test( T2&, typename boost::disable_if<
    std::is_const< T1 >, T1 >::type* = 0 ) {

        std::cout << "non-const" << std::endl;
    }
};

int main() {
    const int ci = 5;
    int i = 6;

    C<char> c;
    c.test(ci);
    c.test(i);
    return 0;
}

ただし、次の同様のコードは正常に機能します。

#include <iostream>
#include <tr1/type_traits>
#include <boost/utility.hpp>

namespace std {
    using namespace tr1;
}

template <typename T1>
struct C {
    template< typename T2 >
    void test( T2&, typename boost::enable_if<
    std::is_const< T2 >, T1 >::type* = 0 ) {

        std::cout << "const" << std::endl;
    }

    template< typename T2 >
    void test( T2&, typename boost::disable_if<
    std::is_const< T2 >, T1 >::type* = 0 ) {

        std::cout << "non-const" << std::endl;
    }
};

int main() {
    const int ci = 5;
    int i = 6;

    C<char> c;
    c.test(ci);
    c.test(i);
    return 0;
}

私が達成したいのは、クラステンプレートで宣言された型に基づいていくつかのメンバー関数を無効/有効にすることです。実際には、テンプレート メンバー関数は必要ありません。SFINAE にのみ追加されます。

誰でも助けてくれますか??

ありがとう!

4

2 に答える 2