专门的模板构造函数不被视为构造函数

Eri*_*ric 3 c++ templates

这段代码:

constexpr uint32_t ticksPerSecond = 100000;

struct ticks {
    uint32_t count;
    template<typename integer>
    constexpr explicit ticks(integer c) : count(c) { }
    explicit inline operator float() {
        return count / (float) ticksPerSecond;
    }
};

template<>
constexpr explicit ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }
Run Code Online (Sandbox Code Playgroud)

给我错误:

timer.hpp:(last line of snippet):
error: only declarations of constructors can be 'explicit'
Run Code Online (Sandbox Code Playgroud)

肯定ticks::ticks 构造函数?

mfo*_*ini 11

错误消息非常清楚,您只能explicit声明中使用(不在定义中).只需从专业化中删除该关键字:

template<>
constexpr ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }
Run Code Online (Sandbox Code Playgroud)