2017-05-15 9 views
1

は、私は、これらの二つの機能を持っている:テンプレートとconst型?

​​

T=std::string場合、これは動作するはずですが、T= const std::string場合、それはしていません。

どうすればこの問題を解決できますか?特にvector<cost std::string>は許可されていないため、可能であってもT=std::stringの場合は問題が発生します。

+0

['std :: remove_const'](http://en.cppreference.com/w/cpp/types/remove_cv)? – StoryTeller

+1

@StoryTellerご意見ありがとうございます。それをどう使うか教えてください。 – justHelloWorld

+1

明白なのは 'const std :: vector :: type>&values'です。しかし、あなたは実際の問題が何であるかを説明するのに十分な詳細を提供していませんでした。 (BTW、 'ベクトル'は許されています、それはちょうど制限されています)。 – StoryTeller

答えて

0

SFINAEを使用していますか?

#include <iostream> 
#include <type_traits> 

template <typename T> 
typename std::enable_if<false == std::is_same<T, T const>::value>::type 
     foo (T & t) 
{ std::cout << "not const T: " << t << std::endl; } 

template <typename T> 
typename std::enable_if<true == std::is_same<T, T const>::value>::type 
     foo (T & t) 
{ std::cout << "const T: " << t << std::endl; } 

int main() 
{ 
    std::string  a {"abc"}; 
    std::string const x {"xyz"}; 

    foo(a); // print not const T: abc (error if you delete the first foo()) 
    foo(x); // print const T: xyx (error if you delete the second foo()) 

    return 0; 
} 
関連する問題