C++:"such strings"类型。使像 fun( "str" ) 一样的函数调用工作
C++: type of "such strings". Making a function call like fun("str") work
>免责声明:这将是蹩脚的。
两个问题:
-
由好奇心驱动的部分:带引号的字符串的确切类型是什么?以前我认为它是一个 C
char[]
字符串在需要时转换为std::string
,但一些type_traits
实验揭示了这一点:std::is_lvalue_reference<decltype ("string")>::value; -> true std::is_object<std::remove_reference<decltype ("string")>>::value; -> true std::is_same<decltype ("string"), std::string&>::value; -> false
-
蹩脚的部分:函数应该采用什么类型的参数才能处理像
fun("str")
这样的调用?这个问题的原因是以下示例由于static_assert
而无法编译:template <typename T> void fun (const T &what) { static_assert(sizeof(T) < 0, "Something unsupported"); } void fun (std::string str) { std::cout << "Copied" << std::endl; } void fun (const std::string &str) { std::cout << "Lvalue" << std::endl; } void fun (std::string &&str) { std::cout << "Rvalue" << std::endl; } int main () { fun ("str"); //static assertion failed return 0; }
此外,注释掉模板会导致
error: call of overloaded 'fun(const char [4])' is ambiguous candidates are: [all three options]
这对我来说似乎并不模棱两可。为什么它不构造一个临时字符串并通过右值引用传递它?
字符串文字"str"
的类型为 const char[4]
。此参数最合适的函数是模板函数,因为它不需要转换为 std::string。
以前我认为它是一个 C
char[]
字符串在需要时转换为std::string
关闭。这是一个在需要时转换为std::string
const char []
。
蹩脚的部分:函数应该采用什么类型的参数才能处理像
fun("str")
这样的调用?
您可以使用std::string
.您可以使用 const char (&) [N]
.您可以使用const char *
.然而。。。
这个问题的原因是以下示例由于
static_assert
而无法编译:
。你必须给重载解决一个机会。当你有
template <typename T> void fun (const T &what) { static_assert(sizeof(T) < 0, "Something unsupported"); }
那么即使字符串文字可以隐式转换为std::string
,你说这个函数可以直接接受字符串文字。当在隐式转换后采用字符串文字的函数和直接采用字符串文字的函数之间进行选择时,将调用直接采用字符串文字的函数。
此外,注释掉模板会导致
error: call of overloaded 'fun(const char [4])' is ambiguous candidates are: [all three options]
在按值和按引用之间选择。让单个非重载函数按值获取std::string
就可以了。让两个重载函数按const &
和&&
引用std::string
也可以。但是按价值和参考太多了,没有理由偏爱一个而不是另一个。
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 递归函数调用在后台工作
- 当在函数调用中递增值时,程序正常工作,但是如果我们在单独的行中增加值而不是呼叫函数,则会出现错误.为什么
- 运算符<<使用temp变量工作,但不直接使用函数调用
- 如何使堆栈在函数调用中工作
- 通过中间函数调用WriteConsole无法正常工作
- 在C/ c++中,当两个函数具有相同的名称,一个函数调用另一个函数时,它将工作
- c++ Lambda——在接下来的函数调用中不工作的字符串参数
- 这个变量参数列表函数调用是如何工作的
- 函数调用在反汇编代码中似乎无法正常工作
- C++:"such strings"类型。使像 fun( "str" ) 一样的函数调用工作