为什么我可以在std::map<std::string, int>中使用const char*作为键?

7

我定义了一个数据结构

std::map<std::string, int> a;

我发现可以像这样传递const char*作为键:

a["abc"] = 1;

哪个函数提供了自动将const char*转换为std::string的类型转换?

1
这样做听起来隐含着会导致难以找到的错误的风险。 - Warren P
1
不,有些隐式转换是可以的。C字符串和std::string在语义上是等价的,只是C字符串是一个无用的垃圾。 - Puppy
@Warren,这在C++中很常见,尤其是对于像std::string这样常用的结构。不过,每次进行查找时都会带来临时std::string构造的性能损耗,你可以享受这种性能损耗。 - Alex
又是一个关于C++光滑斜坡的争论。我猜我还在为上次被它咬了一口而感到不安。 - Warren P
4个回答

16

std::string有一个构造函数,允许从const char*进行隐式转换

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

意味着隐式转换,例如

std::string s = "Hello";

我被允许。

这相当于执行类似以下内容的操作

struct Foo
{
  Foo() {}
  Foo(int) {} // implicit converting constructor.
};

Foo f1 = 42;
Foo f2;
f2 = 33 + 9;

如果你想禁止隐式转换构造函数,你需要将构造函数标记为explicit:
struct Foo 
{
  explicit Foo(int) {}
};

Foo f = 33+9; // error
Foo f(33+9); // OK
f = Foo(33+9); // OK

4

std::string有一个构造函数,它的参数是const char*。

string::string(const char*);

如果构造函数未声明为 explicit,则编译器会在需要调用任何函数时应用一个用户定义的转换。


3
请见字符串构造函数。该构造函数为您的映射键提供了转换。它相当于:
a[std::string("abc")] = 1;

2
在 C++ 中,如果你创建一个只接收一个参数的类构造函数,在没有使用 explicit 的情况下,该参数的类型将会被隐式转换为你的类。 std::string 有一个用于 char * 的这样的构造函数。
是的,这可能会导致一些意外的行为。这就是为什么通常应该在单参数构造函数上加上 explicit,除非你真的希望进行这些静默转换。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接