Hind Forsum
Hind Forsum

Reputation: 10507

c++14 template specialization "used but never defined"

To define template for compile-time recursion, I've to define 2 templates, one normal template, and another one which is for "initial" case like this:

template<int i>
struct Int {};

constexpr auto iter(Int<0>) -> Int<0>;

template<int i>
constexpr auto iter(Int<i>) -> decltype(auto) {
    return iter(Int<i-1>{});
}

int main() {
    decltype(iter(Int<10>{})) a;
    return 0;
}

But gcc gives a warning:

warning: inline function 'constexpr Int<0> iter(Int<0>)' used but never defined
constexpr auto iter(Int<0>) -> Int<0>;

Why there's such a warning?

Upvotes: 2

Views: 1881

Answers (1)

Michael Veksler
Michael Veksler

Reputation: 8475

You have declared the function

constexpr auto iter(Int<0>) -> Int<0>;

but you never defined it, i.e. there is no body.

Maybe you wanted to write:

constexpr auto iter(Int<0>) -> Int<0>
{
    return Int<0>{};
}

But this looks like an XY problem to me. I don't see why you would like to use recursion like that, when you can write a simple loop in a constexpr function instead.

Upvotes: 7

Related Questions