3

这段代码:

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 构造函数?

4

1 回答 1

11

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

template<>
constexpr ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }
于 2013-02-25T14:21:43.773 回答