使用静态constexpr成员的模板特化(函数指针)

时间:2017-04-18 12:27:55

标签: c++ clang++

我有一个类模板,它调用C库中的外部函数。根据专业化(主要是floatdouble),它应该调用不同的函数。我可以通过模板专业化实现这一目标。下面的代码用gcc编译:

// -*- compile-command: "g++ -Wall -Wextra -Werror -pedantic -std=c++14 main.cpp -lm && ./a.out" -*-

#include <iostream>

extern "C" { // for illustration: not using cmath, but linking against libm
    float sinf(float);
    double sin(double);
}

template <typename T> struct Sin_callback;
template <> struct Sin_callback<float> { static constexpr auto sin_cb = sinf; };
template <> struct Sin_callback<double> { static constexpr auto sin_cb = sin; };

template <typename T> class Sin{
    static constexpr Sin_callback<T> m_cb {};
    T m_arg;
public:
    Sin(T&& arg): m_arg(arg) {}
    T calc() { return m_cb.sin_cb(m_arg); }
};

int main(){
    Sin<double> dbl(1.0);
    Sin<float> flt(1.0);
    std::cout.precision(17);
    std::cout << "ref:\t0.84147098480789650665" << std::endl;
    std::cout << "double:\t" << dbl.calc() << std::endl;
    std::cout << "float:\t" << flt.calc() << std::endl;
    return 0;
}

使用gcc 5.4时的输出:

ref:    0.84147098480789650665
double: 0.8414709848078965
float:  0.84147095680236816

但如果我尝试使用clang(3.8和4.0)编译它,则编译失败:

/tmp/main-75afd0.o: In function `Sin<double>::calc()':
main.cpp:(.text._ZN3SinIdE4calcEv[_ZN3SinIdE4calcEv]+0x14): undefined reference to `Sin_callback<double>::sin_cb'
/tmp/main-75afd0.o: In function `Sin<float>::calc()':
main.cpp:(.text._ZN3SinIfE4calcEv[_ZN3SinIfE4calcEv]+0x14): undefined reference to `Sin_callback<float>::sin_cb'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

我无法理解为什么没有实例化专业化。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

clang在静态constexpr数据成员方面遇到问题。

如果将成员转换为函数,则一切正常。

这里我简单地将一个调用操作符应用于代理函数对象。

#include <iostream>

extern "C" { // for illustration: not using cmath, but linking against libm
float sinf(float);
double sin(double);
}

template<typename T>
struct Sin_callback;

template<>
struct Sin_callback<float> {
    template<class...Args>
    constexpr auto operator()(Args &&... args) const { return sinf(std::forward<Args>(args)...); }
};

template<>
struct Sin_callback<double> {
    template<class...Args>
    constexpr auto operator()(Args &&... args) const { return sin(std::forward<Args>(args)...); }
};

template<typename T>
class Sin {

    T m_arg;
public:
    Sin(T &&arg) : m_arg(arg) {}

    T calc() {
        constexpr Sin_callback<T> m_cb{};
        return m_cb(m_arg);
    }
};

int main() {
    Sin<double> dbl(1.0);
    Sin<float> flt(1.0);
    std::cout.precision(17);
    std::cout << "ref:\t0.84147098480789650665" << std::endl;
    std::cout << "double:\t" << dbl.calc() << std::endl;
    std::cout << "float:\t" << flt.calc() << std::endl;
    return 0;
}

预期结果:

ref:    0.84147098480789650665
double: 0.8414709848078965
float:  0.84147095680236816

clang版本:

Apple LLVM version 8.1.0 (clang-802.0.41)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin