简体   繁体   中英

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.

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)" .

So in Debug mode I got it, but in Release I got this: "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

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

edit: forgot #include <iostream> before using cout

As you're calling doSomething() with an integer as parameter, I guess the compiler is just trying to find an arbitrary match here. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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