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

时间:2013-02-25 14:19:27

标签: 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) { }

给我错误:

timer.hpp:(last line of snippet):
error: only declarations of constructors can be 'explicit'

当然ticks::ticks 是构造函数吗?

1 个答案:

答案 0 :(得分:11)

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

template<>
constexpr ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }
相关问题