简体   繁体   English

是否有可能使模板自动将c字符串转换为c ++字符串对象?

[英]is it possible to make template auto cast c-string to c++ string object?

Here the first function compiles and auto converts c char array to c++ string on input, but when I try to generalize that function on other types using template it becomes 'stupid' and says that it do not understand the type. 在这里,第一个函数在输入时将c char数组编译并自动转换为c ++字符串,但是当我尝试使用模板将该函数推广到其他类型时,它变得“愚蠢”,并说它不理解类型。

Is it possible with templates to make functions auto convert from char* to std::string and from w_chart* to std::wstring automatically just as it is happening with plain function definitions ? 模板是否可以使函数自动从char *转换为std :: string,并自动从w_chart *转换为std :: wstring,就像在普通函数定义中一样?

#include <iostream>
#include <vector>
#include <string>

using namespace std;
vector<string> my_vector = { "apples", "bananas", "peaches"};

bool vector_has_item1(vector<string> v, string value_to_check) {
  for (string s : v) {
    if (s == value_to_check) return true;
  }
  return false;
}

template <typename T>
bool vector_has_item2(vector<T> v, T value_to_check) {
  for (T s : v) {
    if (s == value_to_check) return true;
  }
  return false;
}

int main() {
  cout << vector_has_item1(my_vector, "bananas") << endl;
  cout << vector_has_item2(my_vector, "bananas") << endl;
}

Error is: 错误是:

test.cpp:25:48: error: no matching function for call to
 'vector_has_item2(std::vector<std::basic_string<char> >&, const char [8])'

Instead of letting the type T be determined from the second parameter, utilize the builtin typedefs in vector to force T to be determined from the first parameter and force the type of the second parameter to be determined from the first: 而不是让类型T由第二个参数确定,而是利用vector的内置typedef强制从第一个参数确定T并强制从第一个参数确定第二个参数的类型:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;
vector<string> my_vector = { "apples", "bananas", "peaches"};

bool vector_has_item1(vector<string> v, string value_to_check)
{
    return std::find(v.begin(), v.end(), value_to_check) != v.end();
}

template <typename T>
bool vector_has_item2(vector<T> v, typename vector<T>::value_type value_to_check)
{
    return std::find(v.begin(), v.end(), value_to_check) != v.end();
}

int main()
{
  cout << vector_has_item1(my_vector, "bananas") << endl;
  cout << vector_has_item2(my_vector, "bananas") << endl;
}

Implicit conversions (*) are not allowed for parameters whose types depend on template parameters that participate in template parameter deduction (see C++11 §14.8.2.1/4). 对于类型取决于依赖模板参数推导的模板参数的参数,不允许进行隐式转换(*)(请参阅C ++ 11§14.8.2.1/ 4)。

Therefore, when deducing template parameters for 因此,在推导模板参数时

template <typename T>
bool vector_has_item2(vector<T> v, T value_to_check)

T cannot be deduced as std::string because the second argument is not of type std::string , nor a less cv-qualified version of std::string , nor a derived class of std::string . T不能推导出std::string因为第二个参数的类型为不是std::string ,也没有一个较少cv修饰版本std::string ,也没有一个派生类std::string On the other hand, the non-template function 另一方面,非模板功能

bool vector_has_item3(vector<string> v, string value_to_check)

could indeed be called with the arguments my_vector and "bananas" without any problem, because no template parameter deduction is involved, and an implicit conversion is performed on "bananas" in order to initialize the parameter value_to_check . 确实可以使用参数my_vector"bananas"调用,而不会出现任何问题,因为不涉及模板参数推导,并且对"bananas"进行隐式转换以初始化参数value_to_check

(*) Most implicit conversions, anyway---and certainly all user-defined conversions. (*)无论如何, 大多数隐式转换-以及当然所有用户定义的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM