7

I have templated functions in my C++11 Xcode project and some of them have specializations. However, I have discovered that the specializations only get called in debug builds; if I build in release, they are ignored.

I have successfully created a very simple example:

special.h

#include <cstdio>

struct special
{
    template<typename T>
    void call(const T&) { puts("not so special"); }
};

special.cpp

#include "special.h"
#include <string>

template<>
void special::call(const std::string&) { puts("very special"); }

main.cpp

#include "special.h"
#include <string>

int main()
{
    std::string str = "hello world";
    special s;
    s.call(123);
    s.call(str);
}

You can download the project (until somewhere this summer of 2013 at least) to reproduce the issue if you don't want to create it yourself. First run the project with the debug configuration, then run it again in release. The output that I expect is:

not so special
very special

And this is indeed what I get with the Debug build configuration. However, with Release, I get this:

not so special
not so special

Which means the specialized implementation of special::call in special.cpp was ignored.

Why is the result inconsistent? What should I do to ensure that the specialized function is called in release builds?

4

2 に答える 2