简体   繁体   English

为什么const char *强制转换为std :: string有效?

[英]Why does a const char* cast to std::string work?

This cast puzzles me: 这个演员让我困惑:

#include <string>
#include <iostream>
#include <memory>

using namespace std;

int main() {
    string str1 =  (string)"I cast this thing" +  " -- then add this";
    cout << str1 << endl;
}

Can someone explain why this c-style cast to string works (or is allowed)? 有人可以解释为什么这个c样式转换为字符串工作(或被允许)? I compared the generated optimized assembly with that from: 我将生成的优化程序集与以下内容进行了比较

string str1 =  string("I construct this thing") +  " -- then add this";

and they appear to be identical, so I feel like I'm forgetting some c++ semantics that actually allow this kind of cast/construction to be interchanged. 它们似乎是相同的,所以我觉得我忘记了一些c ++语义,它实际上允许这种类型的转换/构造互换。

 std::string str2 =  std::string("I construct this thing") +  " -- then add this";

Yep, if you have a constructor that takes in a single argument like that it will be used to cast the argument type to the object type. 是的,如果你有一个构造函数接受一个像这样的参数,它将用于将参数类型转换为对象类型。 This is why we can pass const char* to functions taking strings. 这就是为什么我们可以将const char*传递给带字符串的函数。

Converting constructor 转换构造函数

A C-style cast will do a const cast and static cast or reinterpret cast whichever is possible. C风格的演员阵容将进行常规演员和静态演员或重新演绎演员。

A static cast will use a user-defined conversion if defined. 如果已定义,静态强制转换将使用用户定义的转换。

std::string has a constructor string(const char *) . std::string有一个构造函数string(const char *)

The syntaxes std::string("something") , static_cast<std::string>("something") and (std::string)"something" are equivalent. 语法std::string("something")static_cast<std::string>("something")(std::string)"something"是等价的。 They will all construct a temporary std::string using the std::string::string(const char *) constructor. 它们都将使用std::string::string(const char *)构造std::string::string(const char *)构造一个临时的std::string The only difference between the syntaxes would be when casting pointers. 语法之间的唯一区别是在转换指针时。

std::string has a constructor in the form of std::string有一个构造函数的形式

basic_string( const CharT* s,
              const Allocator& alloc = Allocator() );

This acts as a conversion operator. 这充当转换运算符。 If you cast a string literal to a std::string it will call this constructor and create a temporary std::string 如果将字符串文字强制转换为std::string ,它将调用此构造函数并创建临时的std::string

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

相关问题 为什么 std::string 没有(显式) const char* 强制转换 - Why std::string does not have (explicit) const char* cast 使用const cast将标准字符串转换为char * - Converting a std string to char* using const cast QString到std :: string到const char *的转换在赋值中不起作用 - QString to std::string to const char* conversion does not work in assignment 为什么“auto”将字符串声明为const char *而不是std :: string? - Why does “auto” declare strings as const char* instead of std::string? std :: string生成链接器错误-const char *不会。 为什么? - std::string generates linker error — const char* does not. why? 为什么std :: string不提供到const char *的转换? - Why does std::string not provide a conversion to const char*? 为什么std :: runtime_error :: what()返回const char *而不是std :: string const& - Why does std::runtime_error::what() return const char* instead of std::string const& 为什么此函数将&#39;const char *&#39;强制转换为&#39;void * const&#39;而不是&#39;const void *&#39; - Why does this function cast 'const char*' to 'void* const' and not 'const void*' 为什么 string 在这里不起作用而 char const* 可以? - Why does string not work here while char const* does? 为什么std :: string {“ const char ptr”}有效? - Why std::string{“const char ptr”} works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM