[英]Qt Creator: different behavior of class template in Debug and Release modes
I'm trying to specialize template member function. 我正在尝试专门化模板成员函数。 The problem is that in Debug mode calls specialization of method, but in Release calls non-specialized function.
问题在于,在Debug模式下,调用方法的特殊化,而在Release模式下,调用非专用函数。
For example, I have class template and specialized method 例如,我有类模板和专门的方法
template <typename T>
class SimpleClass
{
public:
void doSomething(const T& arg)
{
std::cout << "doSomething(const T& arg)" << std::endl;
}
};
template <>
void SimpleClass<double>::doSomething(const double& arg)
{
std::cout << "doSomething(const double& arg)" << std::endl;
}
int main(int argc, char **argv)
{
SimpleClass<double> obj;
obj.doSomething(1);
return 0;
}
I'm expecting, that output will "doSomething(const double& arg)"
. 我期望该输出将为
"doSomething(const double& arg)"
。
So in Debug mode I got it, but in Release I got this: "doSomething(const T& arg)"
因此,在“调试”模式下我明白了,但是在“发行版”中我明白了这一点:
"doSomething(const T& arg)"
My question is why that code behave this way and how can I fix it? 我的问题是,为什么该代码具有这种行为,我该如何解决? Just in case, my .pro file
以防万一,我的.pro文件
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
SOURCES += main.cpp \
mathop.cpp
HEADERS += \
mathop.h
edit: I'm using Qt 5.9.1 编辑:我正在使用Qt 5.9.1
edit: forgot #include <iostream>
before using cout
编辑:使用
cout
之前忘记#include <iostream>
As you're calling doSomething()
with an integer as parameter, I guess the compiler is just trying to find an arbitrary match here. 当您使用整数作为参数调用
doSomething()
时,我猜编译器只是试图在此处查找任意匹配项。 I can't say why debug and release builds behave differently, but I would expect it to work as expected when explicitely using a float value in the call: 我不能说为什么调试版本和发布版本的行为会有所不同,但是当在调用中明确使用浮点值时,我希望它能按预期工作:
obj.doSomething(1.0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.