テンプレートクラスにメンバー関数があるかどうかを確認するにはどうすればよいですか? 質問する

テンプレートクラスにメンバー関数があるかどうかを確認するにはどうすればよいですか? 質問する

クラスで特定のメンバー関数が定義されているかどうかに応じて動作を変更するテンプレートを作成することは可能ですか?

私が書きたいことの簡単な例を以下に示します。

template<class T>
std::string optionalToString(T* obj)
{
    if (FUNCTION_EXISTS(T->toString))
        return obj->toString();
    else
        return "toString not defined";
}

したがって、定義されclass Tている場合toString()はそれを使用し、そうでない場合は使用しません。やり方がわからない不思議な部分は、「FUNCTION_EXISTS」の部分です。

ベストアンサー1

はい、SFINAE を使用すると、特定のクラスが特定のメソッドを提供しているかどうかを確認できます。動作コードは次のとおりです。

#include <iostream>

struct Hello
{
    int helloworld() { return 0; }
};

struct Generic {};    

// SFINAE test
template <typename T>
class has_helloworld
{
    typedef char one;
    struct two { char x[2]; };

    template <typename C> static one test( decltype(&C::helloworld) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
    
int main(int argc, char *argv[])
{
    std::cout << has_helloworld<Hello>::value << std::endl;
    std::cout << has_helloworld<Generic>::value << std::endl;
    return 0;
}

Linux と gcc 4.1/4.3 でテストしました。異なるコンパイラを実行している他のプラットフォームに移植できるかどうかはわかりません。

おすすめ記事