2

重複の可能性:
メンバー関数を条件付きでコンパイルするための std::enable_if

Foo<T>::bar()次のような特定のタイプのメソッドをオーバーロードしようとしてTいます-成功しません。ポインタと回避策をいただければ幸いです。

#include <cstdlib>
#include <iostream>
#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>

template<typename T>
struct Foo
{
    typename boost::enable_if_c<boost::is_same<char,T>::value >::type
    bar();

    typename boost::disable_if_c<boost::is_same<char,T>::value >::type
    bar();
};

template<typename T>
typename boost::disable_if_c<boost::is_same<char,T>::value >::type
Foo<T>::bar()
{
    std::cout << "I am generic ..." << std::endl;
}

template<typename T>
typename boost::enable_if_c<boost::is_same<char,T>::value >::type
Foo<T>::bar()
{
    std::cout << "I am specific ..." << std::endl;
}

int main()
{
    Foo<char> f;
    f.bar();

    return EXIT_SUCCESS;
}

これをideoneでコンパイルすると、次のコンパイラ エラーが発生します。

prog.cpp:13: error: ‘typename boost::disable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’ cannot be overloaded
prog.cpp:10: error: with ‘typename boost::enable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’
prog.cpp:18: error: prototype for ‘typename boost::disable_if_c<boost::is_same::value, void>::type Foo<T>::bar()’ does not match any in class ‘Foo<T>’
prog.cpp:10: error: candidate is: typename boost::enable_if_c<boost::is_same::value, void>::type Foo<T>::bar()
4

1 に答える 1

0

同じパラメーターで enable_if と disable_if を使用しています。同じテンプレート インスタンスを有効または無効にしているように見えますが、コンパイラはそれをオーバーロードしていると考えています。両方のインスタンスで is_same_char を使用しないでください。

于 2012-10-10T15:13:54.290 に答える