1
は、私は、これらの二つの機能を持っている:テンプレートとconst型?
T=std::string
場合、これは動作するはずですが、T= const std::string
場合、それはしていません。
どうすればこの問題を解決できますか?特にvector<cost std::string>
は許可されていないため、可能であってもT=std::string
の場合は問題が発生します。
は、私は、これらの二つの機能を持っている:テンプレートとconst型?
T=std::string
場合、これは動作するはずですが、T= const std::string
場合、それはしていません。
どうすればこの問題を解決できますか?特にvector<cost std::string>
は許可されていないため、可能であってもT=std::string
の場合は問題が発生します。
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;
}
['std :: remove_const'](http://en.cppreference.com/w/cpp/types/remove_cv)? – StoryTeller
@StoryTellerご意見ありがとうございます。それをどう使うか教えてください。 – justHelloWorld
明白なのは 'const std :: vector :: type>&values'です。しかし、あなたは実際の問題が何であるかを説明するのに十分な詳細を提供していませんでした。 (BTW、 'ベクトル'は許されています、それはちょうど制限されています)。 –
StoryTeller