I have define a data structure
我定义了一个数据结构
std::map<std::string, int> a;
I found I can pass const char* as key, like this:
我发现我可以把const char*作为key,如下所示:
a["abc"] = 1;
Which function provides automatic type conversion from const char* to std::string?
哪个函数提供从const char*到std::string的自动类型转换?
4 个解决方案
#1
15
std::string
has a constructor that allows the implicit conversion from const char*
.
string有一个构造函数,它允许从const char*隐式转换。
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
means that an implicit conversion such as
表示隐式转换,如
std::string s = "Hello";
is allowed.
是被允许的。
It is the equivalent of doing something like
这相当于做一些类似的事情
struct Foo
{
Foo() {}
Foo(int) {} // implicit converting constructor.
};
Foo f1 = 42;
Foo f2;
f2 = 33 + 9;
If you wanted to disallow the implicit conversion construction, you mark the constructor as explicit
:
如果您希望不允许隐式转换构造,您可以将构造函数标记为显式:
struct Foo
{
explicit Foo(int) {}
};
Foo f = 33+9; // error
Foo f(33+9); // OK
f = Foo(33+9); // OK
#2
4
There is a constructor for std::string which takes const char* as a parameter.
std::string有一个构造函数,它以const char*作为参数。
string::string(const char*);
Unless the constructor is declared explicit then the compiler will apply one use defined conversion if needed to call any function.
除非构造函数被声明为显式,否则编译器将在需要调用任何函数时应用一个定义的转换。
#3
3
See string constructor. The constructor provides the conversion for the key in your map. It's equivalent to
看到字符串构造函数。构造函数为映射中的键提供转换。它等于
a[std::string("abc")] = 1;
#4
2
In C++ if you make a class constructor that only takes one parameter, then (unless you tell it otherwise with explicit
), that parameter's type will be implicitly convertable to your class.
在c++中,如果您创建的类构造函数只接受一个参数,那么(除非您用显式的方式告诉它),该参数的类型将隐式地可转换到您的类。
std::string
has such a constructor for char *
string有这样一个char *的构造函数
Yes, this can cause some unexpected behavior on occasion. This is why you generally should put explicit
on single-parameter constructors, unless you really want these silent conversions.
是的,这有时会导致一些意想不到的行为。这就是为什么您通常应该在单参数构造函数上使用显式构造函数,除非您确实需要这些静默转换。
#1
15
std::string
has a constructor that allows the implicit conversion from const char*
.
string有一个构造函数,它允许从const char*隐式转换。
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
means that an implicit conversion such as
表示隐式转换,如
std::string s = "Hello";
is allowed.
是被允许的。
It is the equivalent of doing something like
这相当于做一些类似的事情
struct Foo
{
Foo() {}
Foo(int) {} // implicit converting constructor.
};
Foo f1 = 42;
Foo f2;
f2 = 33 + 9;
If you wanted to disallow the implicit conversion construction, you mark the constructor as explicit
:
如果您希望不允许隐式转换构造,您可以将构造函数标记为显式:
struct Foo
{
explicit Foo(int) {}
};
Foo f = 33+9; // error
Foo f(33+9); // OK
f = Foo(33+9); // OK
#2
4
There is a constructor for std::string which takes const char* as a parameter.
std::string有一个构造函数,它以const char*作为参数。
string::string(const char*);
Unless the constructor is declared explicit then the compiler will apply one use defined conversion if needed to call any function.
除非构造函数被声明为显式,否则编译器将在需要调用任何函数时应用一个定义的转换。
#3
3
See string constructor. The constructor provides the conversion for the key in your map. It's equivalent to
看到字符串构造函数。构造函数为映射中的键提供转换。它等于
a[std::string("abc")] = 1;
#4
2
In C++ if you make a class constructor that only takes one parameter, then (unless you tell it otherwise with explicit
), that parameter's type will be implicitly convertable to your class.
在c++中,如果您创建的类构造函数只接受一个参数,那么(除非您用显式的方式告诉它),该参数的类型将隐式地可转换到您的类。
std::string
has such a constructor for char *
string有这样一个char *的构造函数
Yes, this can cause some unexpected behavior on occasion. This is why you generally should put explicit
on single-parameter constructors, unless you really want these silent conversions.
是的,这有时会导致一些意想不到的行为。这就是为什么您通常应该在单参数构造函数上使用显式构造函数,除非您确实需要这些静默转换。