简体   繁体   English

Qt Creator:在调试和发布模式下类模板的不同行为

[英]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.

相关问题 调试和发布模式中的不同结果 - Different Results in Debug and Release Modes 使用qmake / Qt Creator链接调试/发布库 - Linking with a debug/release lib with qmake/Qt Creator 在 Qt Creator 中构建错误,但在调试中没有 - Build error in Qt Creator in release but not in debug 如何检测Qt Creator的目标(调试/发布)(Visual Studio) - How to detect Qt Creator's target (debug/release) (Visual Studio) strip - Qt 创建者在发布时无法识别文件格式,但在调试时无法识别 - strip - file format not recognized with Qt creator on Release but not Debug Qt Creator:单元测试:是否有可能有四种构建代码的方法(调试,发布,调试|发布和测试) - Qt Creator: unit-test: is it possible to have four ways of building a code (debug, release, debug|release and Test) setjmp / longjmp在发行和调试中的不同行为 - Different behavior of setjmp/longjmp in release & debug 在 Qt Creator 中调试与运行。 不同的数值 - Debug vs Run in Qt Creator. Different numerical values QT Creator:程序在调试模式下崩溃,但在发布模式和调试模式下工作,带有基于QThread的程序的断点 - QT Creator : Program crashes in debug mode but working in Release mode and in DEBUG Mode with breakpoints for QThread based program Qt Creator在指定的调试文件夹中创建调试和释放文件夹 - Qt Creator creates both a debug and a release folder inside the designated debug folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM