简体   繁体   English

意外的行为字符串文字

[英]Unexpected behavior string literal

I have two methods: 我有两种方法:

ind addFilter(ind column, const QString & condition);
ind addFilter(ind column, bool condition);

When I use this construction gcc choose second method (with bool condition ) 当我使用这种构造gcc选择第二种方法( bool condition

filters->addFilter(familyNameInd, "М*");

Why std::string convert to bool instead of QString? 为什么将std::string转换为bool而不是QString? Or can I specify all string literals to be QString at compilation? 还是可以在编译时将所有字符串文字指定为QString?

According to the C++ Standard (13.3.3.2 Ranking implicit conversion sequences) 根据C ++标准(13.3.3.2对隐式转换序列进行排名)

2 When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1) — a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and ... 2在比较隐式转换序列(如13.3.3.1中定义)的基本形式时-标准转换序列(13.3.3.1.1)比用户定义的转换序列或省略号转换序列更好,并且。 ..

Relative to your example converting to bool is a standard implicit conversion. 相对于您的示例,转换为bool是标准的隐式转换。 So it s better than user-defined conversion using a conversion constructor. 因此,它比使用转换构造函数的用户定义转换更好。 So if you want that the overloaded function with parameter of type const QString & would be called you have to specify explicitly the conversion from the string literal to a temporary object of type QString: QString( "М*" )`. 因此,如果要调用参数类型为const QString &的重载函数,则必须明确指定从字符串文字到QString:类型的临时对象的转换QString: QString(“М*”)`。

"M*" is a const char* which is convertible to a bool due to implicit conversion rules. “ M *”是一个const char *,由于隐式转换规则,它可以转换为bool。 The implicit conversion will be chosen over the QString class. 隐式转换将在QString类上选择。

You are going to have to call the function like this: 您将必须这样调用该函数:

filters->addFilter(familyNameInd, QString("М*"));

To avoid an unnecessary copy of the QString, consider using the QStringLiteral macro as well: 为了避免不必要地复制QString,请考虑同时使用QStringLiteral宏:

filters->addFilter(familyNameInd, QStringLiteral("М*"));

First of all, your code is broken. 首先,您的代码已损坏。 There is no such an argument and return type as ind . 没有像ind这样的参数和返回类型。 I will assume that you meant int . 我认为你的意思是int

Secondly, POD types have precedence over custom types in C++ when it comes to implicit conversion. 其次,在隐式转换方面,POD类型优先于C ++中的自定义类型。

Why std::string convert to bool instead of QString? 为什么将std :: string转换为bool而不是QString?

I do not know why you think it would be std::string . 我不知道您为什么认为它将是std::string It simply is not, not even in C++. 事实并非如此,甚至在C ++中也是如此。 It is the good old const char[X] where X happens to be three in your case (two letters + terminating nil). 这是很好的旧const char [X],在您的情况下X恰好是3(两个字母+终止nil)。

Or can I specify all string literals to be QString at compilation? 还是可以在编译时将所有字符串文字指定为QString?

No, but you ought to use this in any case when dealing with string literals in Qt regardless of this situation: 不,但是无论如何在Qt中处理字符串文字时,无论如何都应使用此方法:

filters->addFilter(familyNameInd, QStringLiteral("М*"));

For raw string literals like this, please do not use the QString contructor. 对于原始字符串字面这样,请不要使用QString构造器。 It is pointless. 这是没有意义的。 Therefore, you would be writing something like this: 因此,您将编写如下内容:

main.cpp main.cpp中

#include <QString>
#include <QDebug>

int addFilter(int column, const QString & condition) { qDebug() << "Test 1"; }
int addFilter(int column, bool condition) { qDebug() << "Test 2"; }

int main()
{
    addFilter(0, QStringLiteral("foo"));
    return 0;
}

main.pro main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run 生成并运行

qmake && make && ./main

Output 产量

Test 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM