C++ 的含义g++ 中的引号与 MSVC
可能有一个谷歌搜索可以回答这个问题,但就我的一生而言,我想不出一个不会得到数百万个不相关答案的搜索。
因此,
在 MSVC 中,引号内的文本“像这样”被视为 std::string 或 std::string&。在 g++/gcc 中,它似乎总是被视为 const char*。真的?
我找到了一个我想玩的代码片段,它包含
if(NULL == key)
throw exception("Empty key");
在 MSVC/VC++(2008() 中编译得很好,但是当我在 g++ (4.4.3) 上尝试它时,我得到了
no matching functions for calls to std::exception::exception(const char&)
这个工作:
if (NULL == key)
{
std::string estr ("Empty key");
throw exception("Empty key");
}
只是丑陋的。
但这 给我带来了不同的错误:
std::string estr ("");
if (NULL == key)
{
estr = "Empty key";
throw exception("Empty key");
}
我不知道异常()期望什么作为其输入,我确实找到了一些建议 std::string 或 std::string& 的内容,但我丢失了该页面以及我发现的数百万个无用的页面。自从嗯,没有用。有关于异常类、异常使用的各种信息,...
除了我丑陋的修复之外,有没有一种简单的方法来告诉 g++“这是一个 std::string”而不是 const char& ; 并且仍然让 VC++ 满意?(显然我正在尝试从单一源代码中进行交叉编译。)
就此而言,id
throw exception("Empty key");
与
throw "Empty key";
谢谢,
Wes有什么不同?
There's probably a Google search that'll answer this question but for the life of me, I can't think of one that doesn't get millions of unrelated answers.
So,
In MSVC text inside quotes "like this" is taken, I think, as a std::string or, maybe, std::string&. In g++/gcc it always seems to be taken as const char*. True?
I found a code snippet I wanted to play with and it contains
if(NULL == key)
throw exception("Empty key");
compiles just fine in MSVC/VC++(2008( but when I try it on g++ (4.4.3) I get
no matching functions for calls to std::exception::exception(const char&)
I got this to work:
if (NULL == key)
{
std::string estr ("Empty key");
throw exception("Empty key");
}
But that's just plain ugly.
This got me different errors:
std::string estr ("");
if (NULL == key)
{
estr = "Empty key";
throw exception("Empty key");
}
I have no clue what exception() expects as its input. I did find something that suggested std::string or maybe std::string& but I lost that page and the millions of unhelpful pages I've found since are, well, useless. Have all kinds of info on exception class, exception use,....
Short of my ugly fix, is there a simple way to tell g++ that "this is a std::string" not a const char& and still keep VC++ happy? (obviously I'm trying to do cross compilable code from single source.)
And for that matter, how id
throw exception("Empty key");
different from
throw "Empty key";
Thanks,
Wes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
字符串文字始终具有
char
数组的类型,其大小足以包含文字中带有空终止符的字符。因此“Empty key”
的类型为char[10]
。如果需要,可以将其隐式转换为char const *
或std::string
。该错误是因为
exception
旨在作为异常类型的基类,而不是直接实例化的东西。您应该抛出
中定义的类型之一,例如std::runtime_error
(可以使用字符串构造),或者定义您自己的继承类型std::Exception
并覆盖what()
。我猜测 Microsoft 已经向 std::exception 添加了一个非标准构造函数。他们喜欢以奇怪的方式扩展语言。
A string literal always has the type of an array of
char
, with a size just large enough to contain the characters in the literal with a null terminator. So"Empty key"
has the typechar[10]
. This can be implicitly converted to eitherchar const *
orstd::string
if required.The error is because
exception
is intended as a base class for exception types, not something you instantiate directly. You should throw one of the types defined in<stdexcept>
such asstd::runtime_error
(which can be constructed using a string), or define your own type that inheritsstd::exception
and overrideswhat()
.I'm guessing that Microsoft has added a non-standard constructor to
std::exception
. They like to extend the language in strange ways.std::exception 是基类。
尝试抛出 std::runtime_error 。
std::exception is the base class.
Try throwing a std::runtime_error instead.
避免通过
std::string
类型抛出异常,因为它们本身可以抛出异常。如果发生这种情况,您最终会得到未定义的行为。抛出
中定义的标准异常,或者让您自己的异常类从std::exception
类派生并抛出它。并重写what()< /code> 方法添加适当的异常描述。
另外,始终按值
抛出
并按引用catch
。Avoid throwing exceptions by type
std::string
because they themselves can throw an exception.And if that happens you end up with an Undefined Behavior.Throw standard exceptions defined in
<stdexcept>
or have your own exception class derive fromstd::exception
class and throw it.And override thewhat()
method to add appropriate description of the exception.Also, Always
throw
by value andcatch
by reference.根据标准,
“like this”
始终被视为const char
数组。这不是这里的问题。真正发生的情况是,std::exception
仅具有默认构造函数,这也是根据标准。通常会包含一个采用文本字符串的附加构造函数,但此构造函数在 VC++ 和 g++ 中的工作方式不同,如您的示例所示。我这里没有 g++ 文档,但是从您的示例来看,它看起来像将非标准构造函数定义为:
explicit exception(const std::string&);
而 VC++ 定义了非- 标准构造函数为:
显式异常(const char*);
"like this"
is always regarded as an array ofconst char
, according to the standard. This is not the problem here. What really happens is thatstd::exception
only has the default constructor, once again according to the standard. It is usual to include an additional constructors taking a text string, but this constructor works differently in VC++ and g++, as your example shows.I haven't got the g++ documents here but, from your examples, it looks like it defines that non-standard constructor as:
explicit exception(const std::string&);
while VC++ defines the non-standard constructor as:
explicit exception(const char*);