I’m trying to implement a class template with static member functions that can handle different types. My code looks like this (very simplified example):
<code>#include <array>
struct Foo {
template<typename T>
static constexpr size_t foo(T v);
};
template<>
constexpr size_t Foo::foo(double v) {
return sizeof(v);
}
template<int count>
constexpr size_t Foo::foo(const std::array<int, count>& v) {
return sizeof(int) * v.size();
}
</code>
<code>#include <array>
struct Foo {
template<typename T>
static constexpr size_t foo(T v);
};
template<>
constexpr size_t Foo::foo(double v) {
return sizeof(v);
}
template<int count>
constexpr size_t Foo::foo(const std::array<int, count>& v) {
return sizeof(int) * v.size();
}
</code>
#include <array>
struct Foo {
template<typename T>
static constexpr size_t foo(T v);
};
template<>
constexpr size_t Foo::foo(double v) {
return sizeof(v);
}
template<int count>
constexpr size_t Foo::foo(const std::array<int, count>& v) {
return sizeof(int) * v.size();
}
However, when I try to compile it, I get the following error:
<code><source>:16:23: error: out-of-line definition of 'foo' does not match any declaration in 'Foo'
16 | constexpr size_t Foo::foo(const std::array<int, count>& v) {
| ^~~
1 error generated.
Compiler returned: 1
</code>
<code><source>:16:23: error: out-of-line definition of 'foo' does not match any declaration in 'Foo'
16 | constexpr size_t Foo::foo(const std::array<int, count>& v) {
| ^~~
1 error generated.
Compiler returned: 1
</code>
<source>:16:23: error: out-of-line definition of 'foo' does not match any declaration in 'Foo'
16 | constexpr size_t Foo::foo(const std::array<int, count>& v) {
| ^~~
1 error generated.
Compiler returned: 1
How can I specialise a template with another template?