use*_*911 12 c++ string templates implicit-conversion
这是C++程序:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int test_string(const string & str) {
return str.size();
}
void main() {
test_string(""); //can compile
vector<string> v;
string sum = accumulate(v.cbegin(), v.cend(), ""); //cannot compile
}
Run Code Online (Sandbox Code Playgroud)
我想使用隐式转换,从const char *
以string
仿制STL函数的调用accumulate
.我知道转换const char *
为字符串不是显式的,因此我们可以将const char *
参数传递给需要string
类型的调用.这可以通过上述test_string
功能证明.但当我做同样的事情时accumulate
,编译器抱怨:
Run Code Online (Sandbox Code Playgroud)error C2440: '=': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
代码工作只有当我更换""
使用string("")
.我不明白为什么隐式转换适用于我的自定义函数但不起作用accumulate
.你能解释一下吗?非常感谢.
PS:我正在使用Visual Studio 2015.
son*_*yao 12
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
Run Code Online (Sandbox Code Playgroud)
这意味着模板参数T
是从传入的参数(即""
)中推导出来的.然后它会const char*
.另一方面,编译器如何执行隐式转换?哪种类型应该是目标类型?
您可以std::string
显式传递,或明确指定模板参数.例如
// pass a std::string exactly
string sum = accumulate(v.cbegin(), v.cend(), string(""));
// T is specified as std::string explicitly
// "" will be implicitly converted to std::string
string sum = accumulate<decltype(v.cbegin()), string>(v.cbegin(), v.cend(), "");
Run Code Online (Sandbox Code Playgroud)
Cur*_*ous 11
template<class InputIt, class T>
T accumulate(InputIt first, InputIt last, T init)
{
for (; first != last; ++first) {
init = init + *first;
}
return init;
}
Run Code Online (Sandbox Code Playgroud)
当你以你的方式调用函数时,InputIt
将被推断为a vector<string>::const_iterator
并且T
将被推断为a const char*
.正如你在for循环中看到的那样,执行"累积"的代码行就是这个
init = init + *first
Run Code Online (Sandbox Code Playgroud)
这里的赋值的右手边*first
将评估的string&
和init
将评估的const char*
.然后你将使用std::string::operator+
它将连接const char*
和std::string
实例来std::string
回来.然后你试图分配std::string
一个const char*
变量.这不合法.
这将无法正常工作,std::string
对象是不隐式转换或分配到const char*
,情况正好相反.然而.
要解决此问题,请将您的代码更改为以下内容(请注意,我将字符串文字后缀为a s
,这是用户定义文字的C++ 14语法(在本例中计算结果为a std::string
)http://en.cppreference.com/W/CPP /串/ basic_string的/操作员%22%22S
int main() {
using namespace std::string_literals;
vector<string> v;
string sum = accumulate(v.cbegin(), v.cend(), ""s);
}
Run Code Online (Sandbox Code Playgroud)
同样如评论中所述,void main()
改为int main()
.有关更多信息,请参阅C和C++中main()返回的内容?