Undefined function error when calling a static constexpr function with CRTP - c++

I'm trying to understand the reason for the compilation error with my CRTP implementation in this example:
template<class T>
struct Interface
{
static constexpr int func();
static constexpr int func2();
};
template<class T>
struct Default : public Interface<T>
{
constexpr static int impl_func()
{
return 0;
}
constexpr static int impl_func2()
{
return Default<T>::func() + 2; <----------- (A)
}
};
struct Derived : public Default<Derived>
{
static constexpr int impl_func2()
{
// NOTE: Using impl_func instead of func here compiles
return func() + 4; <-------------- (B)
}
};
struct Derived2 : public Default<Derived2>
{
};
template <typename T> constexpr int Interface<T>::func()
{
return T::impl_func();
}
template <typename T> constexpr int Interface<T>::func2()
{
return T::impl_func2();
}
int main()
{
constexpr int ret = Derived::func2();
static_assert(ret == 4);
constexpr int ret2 = Derived2::func2();
static_assert(ret2 == 2);
}
Specifically why is (A) ok while (B) gives me this error:
test3.cpp:45:19: error: constexpr variable 'ret' must be initialized by a constant expression
constexpr int ret = Derived::func2();
^ ~~~~~~~~~~~~~~~~
test3.cpp:29:16: note: undefined function 'func' cannot be used in a constant expression
return func() + 4;
Replacing (B) with return impl_func() + 4 compiles successfully. But neither Derived::func() nor Default<Derived>::func() work. If (B) needs to be impl_func() then is there something different about the fact that (A) is in a templated class that allows it to compile successfully?

With constexpr function, definition should be available when used...
reordering function definition solve the issue:
template<class T>
struct Interface
{
static constexpr int func();
static constexpr int func2();
};
template <typename T> constexpr int Interface<T>::func()
{
return T::impl_func();
}
template <typename T> constexpr int Interface<T>::func2()
{
return T::impl_func2();
}
// Remaining code.
Demo

Related

if constexpr block not compiling

I have a class that can inherit from an empty struct or from a struct with some members, depending on a bool. Using that same bool, I am adding an if constexpr block to access the members of the base class, but I am getting a compiler error
struct A{};
struct B{};
struct C{};
template <typename J0, typename J1>
struct HasFriction {
constexpr static bool value = false;
};
template <> struct HasFriction<A,B> {
constexpr static bool value = true;
};
template <bool> struct Friction { };
template <> struct Friction<true> { int value = 4; };
template <typename J0, typename J1>
struct MyStruct : public Friction<HasFriction<J0, J1>::value> {};
int main()
{
if constexpr (HasFriction<A, C>::value) {
MyStruct<A,C> f;
return f.value;
} else {
MyStruct<A,C> no_f;
return 0;
}
}
Why is this not working, and how should I fix it?
Why is this not working and how should I fix it?
In order to work if constexpr (i.e. discard the false statement at compile time), you need to make it template depended.
From cppreference.com, under the if constexpr section:
Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive:
void f() {
if constexpr(false) {
int i = 0;
int *p = i; // Error even though in discarded statement
}
}
Therefore, the fix is to wrap the statement into a template function, as follows:
template<typename T1, typename T2> auto func()
{
if constexpr (HasFriction<T1, T2>::value) {
MyStruct<A, C> f;
return f.value;
}
else {
MyStruct<A, C> no_f;
return 0;
}
}
Now in main()
func<A, B>();
See a demo

Partial template specialization of 2nd parameter

I'm working on a C++11 wrapper around a C api. The C api offers a bunch of getters for various types, with a different name for each type. Values are retrieved by array of a given size, known at compilation.
I want to give the type and the array size by template, to call the right function.
#include <string>
#include <iostream>
template <typename T>
struct make_stop {
constexpr static bool value = false;
};
class Foo
{
public:
Foo() : i(42) {}
template<typename T, size_t n>
T get();
private:
int i = 0;
};
template<typename T, size_t n>
T Foo::get() { static_assert(make_stop<T>::value); return T(); }
template<int, size_t n>
int Foo::get() { return i + n; }
int main() {
Foo foo;
int i = foo.get<int, 4>();
double f = foo.get<double, 2>();
return 0;
}
But it fails to match the right function
main.cpp:26:5: error: no declaration matches 'int Foo::get()'
int Foo::get() { return i + n; }
^~~
main.cpp:15:7: note: candidate is: 'template<class T, long unsigned int n> T Foo::get()'
T get();
its a bit vauge from your question, but assuming you are wanting to index into some c- arrays and return the value at I you can't specialize function templates like you want, but you can use some tags instead, something like..
class Foo
{
public:
Foo() : is{1,2,3,4,5,6,7,8,9,10},ds{1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1} {}
template <typename T> struct type_c{};
template <size_t I> struct int_c{};
template<typename T,size_t I>
auto get()
{ return get_impl(type_c<T>(),int_c<I>()); }
private:
template <size_t I>
auto get_impl(type_c<int>,int_c<I>)
{ return is[I]; }
template <size_t I>
auto get_impl(type_c<double>,int_c<I>)
{ return ds[I]; }
int is[10];
double ds[10];
};
int main() {
Foo foo;
int i = foo.get<int,0>();
double d = foo.get<double,2>();
std::cout << i << " " << d << std::endl;
return 0;
}
Demo
If I understood you correctly you want to partially specialize get for T. Unfortunately partial specialization for methods is not allowed by the standard. You can however get around this with a static method on a class templated by T and specializing the class.
Like this:
template <class T> struct Foo_helper;
struct Foo
{
Foo() : i{42} {}
template<class T, std::size_t N>
T get()
{
return Foo_helper<T>::template get<N>(*this);
}
int i = 0;
};
template <class T> struct Foo_helper {};
// specialize Foo_helper for each type T you wish to support:
template <> struct Foo_helper<int>
{
template <std::size_t N>
static int get(const Foo& foo) { return foo.i + N; }
};
template <> struct Foo_helper<double>
{
template <std::size_t N>
static double get(const Foo& foo) { return foo.i + N; }
};
int main()
{
Foo foo{};
int i = foo.get<int, 4>();
double d = foo.get<double, 2>();
}

Size of std::array in class template depending on template parameter

I have a the following class template
template<int N>
constexpr int arraySize() { return arraySize<N-1>() + N; }
template<>
constexpr int arraySize<0>() { return 0; }
template<int C>
class MyClass {
public:
std::array<int, arraySize<C>()> arr;
};
int main() {
MyClass<3> cls;
std::cout << cls.arr.size() << std::endl; // Output: 6
}
Everything works but I would like to have calculateArraySize<N>() as a member function. I've tried the following:
template<int C>
class MyClass {
public:
static constexpr int arraySize();
std::array<int, MyClass<C>::arraySize()> arr;
};
template<int C>
constexpr int MyClass<C>::arraySize(){ return MyClass<C-1>::arraySize() + C; }
template<>
constexpr int MyClass<0>::arraySize() { return 0; }
Results in the following error:
fatal error: recursive template instantiation exceeded maximum
depth of 1024
std::array::arraySize()> arr;
template<int C>
class MyClass {
public:
template<int N>
static constexpr int arraySize();
std::array<int, MyClass::arraySize<C>()> arr;
};
template<int C>
template<int N>
constexpr int MyClass<C>::arraySize(){ return MyClass::arraySize<N-1>() + N-1; }
template<int C>
template<>
constexpr int MyClass<C>::arraySize<0>() { return 0; }
Gives the following error:
tmp.cc:19:27: error: cannot specialize (with 'template<>') a member of an
unspecialized template
constexpr int MyClass::arraySize<0>() { return 0; }
Is it possible to achieve the desired behaviour? Solutions using C++14/C++17 features (I guess it should be possible usinn if-constexpr) are welcomed but won't solve my particular problem since only C++11 is available.
You can move the function into the class and the specialize the entire class for the base case. That looks like:
template<int C>
class MyClass {
public:
static constexpr int arraySize(){ return MyClass<C-1>::arraySize() + C; }
std::array<int, MyClass<C>::arraySize()> arr;
};
template<>
class MyClass<0> {
public:
static constexpr int arraySize(){ return 0; }
};
int main() {
MyClass<3> cls;
std::cout << cls.arr.size() << std::endl; // Output: 6
}
Live Example
You can also use a member variable instead of a member function.
template <int C>
class MyClass {
public:
static constexpr int array_size = MyClass<C-1>::array_size + C;
std::array<int, array_size> arr;
};
template <>
class MyClass<0> {
public:
static constexpr int array_size = 0;
};

How to check for static member variable template?

I need to define a class, foo, with a static member variable template, foo::static_variable_template<T>. This member should only exist when T fulfills certain requirements. For example, when the constexpr static function T::constexpr_static_function() exists. Otherwise, foo::static_variable_template<T> should not exist. Moreover, I would like to be able to test for the existence of foo::static_variable_template<T> at compile-time via SFINAE.
Here is an approximation of what I would like to do:
#include <iostream>
struct foo
{
template<class T>
static constexpr int static_variable_template =
T::constexpr_static_function();
// XXX this works but requires a second defaulted template parameter
// template<class T, int = T::constexpr_static_function()>
// static constexpr int static_variable_template =
// T::constexpr_static_function();
};
struct has_constexpr_static_function
{
static constexpr int constexpr_static_function() { return 42; }
};
struct hasnt_constexpr_static_function
{
};
template<class T, class U,
int = T::template static_variable_template<U>>
void test_for_static_variable_template(int)
{
std::cout << "yes it has\n";
}
template<class T, class U>
void test_for_static_variable_template(...)
{
std::cout << "no it hasn't\n";
}
int main()
{
test_for_static_variable_template<foo, has_constexpr_static_function>(0);
test_for_static_variable_template<foo, hasnt_constexpr_static_function>(0);
}
This approximation nearly works, but only if foo::static_variable_template has a second, defaulted template parameter. Because this second parameter is an implementation detail, I'd like to hide it from the public interface of foo::static_variable_template.
Is this possible in C++17?
I am not sure if your intent is to initialise foo::static_variable_template with 0 if T::constexpr_static_function() is missing or you want to disable it entirely. In case of the former, this might be useful. For example, this (clunky) solution works (requires C++17 for if constexpr; note that your variable is now a function):
#include <iostream>
template <typename T>
class has_func
{
typedef char does;
typedef long doesnt;
template <typename C> static does test( decltype(&C::constexpr_static_function) );
template <typename C> static doesnt test(...);
public:
static constexpr bool value()
{
return sizeof(test<T>(0)) == sizeof(char);
}
};
struct foo
{
template<class T>
static constexpr int static_variable_template()
{
if constexpr (has_func<T>::value())
{
return T::constexpr_static_function();
}
return 0;
}
// XXX this works but requires a second defaulted template parameter
// template<class T, int = T::constexpr_static_function()>
// static constexpr int static_variable_template =
// T::constexpr_static_function();
};
struct has_constexpr_static_function
{
static constexpr int constexpr_static_function() { return 42; }
};
struct hasnt_constexpr_static_function
{
};
template<class T, class U>
void test_for_static_variable_template(...)
{
if constexpr (has_func<U>::value())
{
std::cout << "yes it has\n";
}
else
{
std::cout << "no it hasn't\n";
}
}
int main()
{
std::cout << foo::static_variable_template<has_constexpr_static_function>() << "\n";
std::cout << foo::static_variable_template<hasnt_constexpr_static_function>() << "\n";
/// Original test
test_for_static_variable_template<foo, has_constexpr_static_function>(0);
test_for_static_variable_template<foo, hasnt_constexpr_static_function>(0);
}
Prints
42
0
yes it has
no it hasn't
Tested with clang 5.0.1.
In case you want to disable foo::static_variable_template entirely, you might need to use std::enable_if:
#include <iostream>
template <typename T>
class has_func
{
typedef char does;
typedef long doesnt;
template <typename C> static does test( decltype(&C::constexpr_static_function) );
template <typename C> static doesnt test(...);
public:
static constexpr bool value()
{
return sizeof(test<T>(0)) == sizeof(char);
}
};
struct foo
{
template<class T, typename std::enable_if<has_func<T>::value()>::type ...>
static constexpr int static_variable_template()
{
if constexpr (has_func<T>::value())
{
return T::constexpr_static_function();
}
return 0;
}
// XXX this works but requires a second defaulted template parameter
// template<class T, int = T::constexpr_static_function()>
// static constexpr int static_variable_template =
// T::constexpr_static_function();
};
struct has_constexpr_static_function
{
static constexpr int constexpr_static_function() { return 42; }
};
struct hasnt_constexpr_static_function
{
};
template<class T, class U>
void test_for_static_variable_template(...)
{
if constexpr (has_func<U>::value())
{
std::cout << "yes it has\n";
}
else
{
std::cout << "no it hasn't\n";
}
}
int main()
{
std::cout << foo::static_variable_template<has_constexpr_static_function>() << "\n";
// We can't print this because it doesn't exist.
// std::cout << foo::static_variable_template<hasnt_constexpr_static_function>() << "\n";
/// Original test
test_for_static_variable_template<foo, has_constexpr_static_function>(0);
test_for_static_variable_template<foo, hasnt_constexpr_static_function>(0);
}
In this line of thought, I am not sure if you can disable a static template variable with std::enable_if. To quote the great Riemann, "I have for the time being, after some fleeting vain attempts, provisionally put aside the search for this..."

CRTP compiling error

The following will compile with GCC 5.2 but not with Visual Studio 2015.
template <typename Derived>
struct CRTP {
static constexpr int num = Derived::value + 1;
};
struct A : CRTP<A> {
static constexpr int value = 5;
};
It complains that A does not have a member named value.
How to fix the code so that it compiles on both compilers? Or is it illegal altogether?
Try making it a constexpr function instead. The way you have it setup now attempts to access an incomplete type.
Since a templated member function will only be initialized upon first being used, type A will be fully defined by that point.
#include <iostream>
template <typename Derived>
struct CRTP {
static constexpr int num() { return Derived::value + 1; }
};
struct A : CRTP<A> {
static constexpr int value = 5;
};
int main()
{
std::cout << A::num();
return 0;
}
See it live here
The problem is here:
template <typename Derived>
struct CRTP {
static constexpr int num = Derived::value + 1;
↑↑↑↑↑↑↑↑↑
};
At the time of the instantiation of CRTP<A>, A isn't a complete class yet, so you can't actually access static members of it.
One workaround is to pass in num as a separate template argument:
template <typename Derived, int N>
struct CRTP {
static constexpr int num = N;
};
struct A : CRTP<A, 5> {
};