3

私はこれをあまりにも長い間見つめていたと思いますが、ここでエラーを見つけることができません:

struct
{
    bool empty() const
    {
       return true;
    }
} hasEmpty;

template<typename T>
struct has_empty
{
private:
    template<typename U, U>
    class check {};

    template<typename C>
    static char f(check<void (C::*)() const, &C::empty> *);

    template<typename C>
    static long f(...);

public:
    static const bool value = (sizeof(f<T>(nullptr)) == sizeof(char));
};

template<typename T>
typename std::enable_if<has_empty<T>::value>::type foo(const T& t)
{

}

void x()
{
    foo(hasEmpty);
}

Visual Studio 2012 レポート:

error C2893: Failed to specialize function template 'std::enable_if<has_empty<T>::value>::type foo(const T &)'
1>          With the following template arguments:
1>          '<unnamed-type-hasEmpty>'

(注:ここで説明されているように、このテストの新しい C++11 バージョンが非常に気に入ってますが、VS2012 はまだ constexpr をサポートしていません。)

4

1 に答える 1

3

あなたのhasEmpty::emptyメソッドは以下を返しますbool

struct 
{
    bool empty() const
    {
       return true;
    }
} hasEmpty;

ただし、トレイトは、を返すメンバー関数ポインタを使用しますvoid。その置換は常に失敗します。これを変更する必要があります:

template<typename C>
ctatic char f(check<void (C::*)() const, &C::empty> *);

このため:

template<typename C>
static char f(check<bool (C::*)() const, &C::empty> *);

それは私のためにコンパイルされます。

于 2012-08-18T20:01:06.067 に答える