2016-11-12 5 views
0

私は次の問題に取り組もうとしています:ifステートメントを実行したいのですが、テンプレートの引数が特定のオブジェクトであるか、そうでない場合は、オブジェクトのメンバー関数を呼び出します。のは、私がstd::stringstd :: is_sameを使用してifステートメントでクラスメンバーにアクセス

スニペットをしたいとしましょう:

#include <iostream> 
#include <string> 

template <typename T> 
void is_string(const T& arg) { 
    if (std::is_same<T, const std::string&>::value) 
     std::cout << arg.length() << std::endl; 
    else 
     std::cout << "The argument is not a string" << std::endl; 
} 

int main() { 
    is_string(0); 
    return 0; 
} 

それは次のエラーで、コンパイルされません。

types.cpp: In instantiation of ‘void is_string(const T&) [with T = int]’: 
types.cpp:13:13: required from here 
types.cpp:7:13: error: request for member ‘length’ in ‘arg’, which is of non-class type ‘const int’ 
    std::cout << arg.length() << std::endl; 

私は私が達成しようとしていることはないかもしれないことを数えますC++ 11では可能ですが、そのようなことをする方法についていくつか提案していただきたいと思います。

答えて

1

通常のifステートメントでは、両方のブランチが有効なコードでなければなりません。あなたのケースではint.length()は理にかなっていません。 Cの++で

あなたは、単にconstexpr ifを使用することができ1Z:C++ 11(またはそれ以前)では

if constexpr(std::is_same<T, const std::string&>::value) 
    std::cout << arg.length() << std::endl; 
else 
    std::cout << "The argument is not a string" << std::endl; 

demo

をあなたが同様の結果を達成するために過負荷を採用することができます。

void foo(std::string const& str){ 
    std::cout << str.length() << std::endl; 
} 

template<typename T> 
void foo(T const&){ 
    std::cout << "The argument is not a string" << std::endl; 
} 

template <typename T> 
void is_string(const T& arg) { 
    foo(arg); 
} 

demo

2
void is_string(const std::string& arg) { 
    std::cout << arg.length() << std::endl; 
} 

template <typename T> 
void is_string(const T& arg) { 
    std::cout << "The argument is not a string" << std::endl; 
} 

コンパイラがif constexprをサポートしているかどうかを確認してください。

関連する問題