Eric
Eric

Reputation: 97631

Specialized template constructors not seen as constructors

This code:

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) { }

Gives me the error:

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

Surely ticks::ticks is the constructor?

Upvotes: 3

Views: 119

Answers (1)

mfontanini
mfontanini

Reputation: 21900

The error message is pretty clear, you can only use explicit in declarations (not in definitions). Just remove that keyword from the specialization:

template<>
constexpr ticks::ticks<float>(float s) : count(s * ticksPerSecond) { }

Upvotes: 11

Related Questions