template class that derives from same template - c++

I have an equivalent to following code:
struct Empty {
static constexpr int id = 0;
};
template <typename Self, typename Base = Empty> struct Compound : public Base
{
int get_id() const
{
return Self::id;
}
};
struct A : Compound<A>
{
static constexpr int id = 0xa;
};
struct B : Compound<B, A>
{
static constexpr int id = 0xb;
};
template <typename T, typename Base> int get_id(const Compound<T, Base> &c)
{
return c.get_id();
}
int test_a()
{
A var;
return get_id(var);
}
int test_b()
{
B var;
return get_id(var);
}
test_b doesn't compile with following error:
error: no matching function for call to 'get_id(B&)'
return get_id(var);
^
note: candidate: template<class T, class Base> int get_id(const Compound<T, Base>&)
template <typename T, typename Base> int get_id(const Compound<T, Base> &c)
^
note: template argument deduction/substitution failed:
note: 'const Compound<T, Base>' is an ambiguous base class of 'B'
return get_id(var);
I understand why that is. B is derived and is convertible to both Compound<B, A> and Compound<A, Empty>
I'm wondering if it is possible to change (within context of C++14) Compound template and get_id() function such that it would return 0xa for A and 0xb for B and would work for arbitrarily long chains of inheritance.
I know that this can be easily solved with virtual function that is overridden in A and B, but I would like to avoid that if possible. Everywhere these types are used they are known and fixed at compile time so there shouldn't be a need to incur run-time overhead.

Just keep it simple:
template <class T>
auto get_id(T const& c) -> decltype(c.get_id())
{
return c.get_id();
}
You don't need c to be some Compound, you really just want it to have a get_id() member function.

It's not clear from your post why need to go the route of Compound in get_id. You can simply use:
template <typename T> int get_id(T const& c)
{
return T::id;
}

Related

C++ template function dependent typename not recognized

In the following mcve:
template <typename T> class Class { public: class MemberClass {}; };
#include <list>
template <typename T>
Class<T> func(const typename Class<T>::MemberClass& start,
const typename Class<T>::MemberClass& finish)
{
Class<T> result; return result;
}
int main ()
{
Class<int>::MemberClass i, j;
Class<int> L2; L2 = func(i, j);
return 0;
}
I find that the compiler does not recognize the function as something that can take the given arguments. In Visual Studio the error is
1>C:\...\main.cpp(15,25): error C2672: 'func': no matching overloaded function found
1>C:\...\main.cpp(15,34): error C2783: 'Class<T> func(const Class<T>::MemberClass &,const Class<T>::MemberClass &)': could not deduce template argument for 'T'
1>C:\...\main.cpp(6): message : see declaration of 'func'
In g++, here are the errors:
main.cpp:15:34: error: no matching function for call to 'func(Class<int>::MemberClass&, Class<int>::MemberClass&)'
15 | Class<int> L2; L2 = func(i, j);
| ^
main.cpp:6:10: note: candidate: 'template<class T> Class<T> func(const typename Class<T>::MemberClass&, const typename Class<T>::MemberClass&)'
6 | Class<T> func(const typename Class<T>::MemberClass& start,
| ^~~~
main.cpp:6:10: note: template argument deduction/substitution failed:
main.cpp:15:34: note: couldn't deduce template parameter 'T'
15 | Class<int> L2; L2 = func(i, j);
| ^ ^
I'm sure there's another way to write the function (it could return a MemberClass not a Class), but, well, this should be doable. How can I make that happen?
If you want to do it without specifying a template type for func, you can make the type of Class<T> known to the MemberClass and have the compiler resolve T within the func function template to Class<T>::MemberClass instead of the T within Class<T>.
This is very easy for the compiler, since it is the type of the arguments of func:
template <typename T>
class Class {
public:
class MemberClass { public: using ParentClass = Class<T>; };
};
#include <list>
template <typename T>
typename T::ParentClass func(const T& start,
const T& finish)
{
typename T::ParentClass result; return result;
}
int main ()
{
Class<int>::MemberClass i, j;
Class<int> L2; L2 = func(i, j);
return 0;
}
Quite literally, every possible T that matches your Class has a MemberClass type. The compiler is not going to look inside all of them to find a match, because it'd have to instantiate templates just to see their contents, and it would potentially match too many things. So the langauge simply doesn't look inside like that.
The way you can solve this is to tell the compiler what T is when you call it, here, providing an explicit int template argument:
func<int>(i, j);
In
template <typename T>
Class<T> func(const typename Class<T>::MemberClass&,
const typename Class<T>::MemberClass&);
T is not deducible (left of ::).
You have several options:
Make a friend non-template function inside MemberClass (will be found by ADL):
template <typename T> class Class {
public:
class MemberClass {
friend Class func(const MemberClass& start, const MemberClass& finish)
{
Class result; return result;
}
};
};
Demo
Be more generic of accepted type (you need here to give a way to retrieve Class from MemberClass):
template <typename T> class Class {
public:
class MemberClass {
public:
using ParentClass = Class;
};
};
template <typename T>
typename T::ParentClass func(const T& start, const T& finish)
{
typename T::ParentClass result; return result;
}
Demo

Accessing functions of a derived crtp class from the base calls

I need to be able to access a static method of the derived class, from within a base CRTP class. Is there a way in which I can achieve this?
Here is example code:
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__), bool> = true
template<typename Derived>
struct ExpressionBase {
Derived& derived() { return static_cast<Derived&>(*this); }
const Derived& derived() const { return static_cast<const Derived&>(*this); }
constexpr static int size()
{
return Derived::size();
}
template<typename T, REQUIRES(size() == 1)>
operator T() const;
};
struct Derived : public ExpressionBase<Derived>
{
constexpr static int size()
{
return 1;
}
};
Deriving from ExpressionBase<Derived> involves the instantiation of ExpressionBase<Derived>, therefore involves the declaration of the entity
template<typename T, REQUIRES(size() == 1)>
operator T() const;
Here, std::enable_if_t got a template argument that is ill-formed (because Derived isn't complete yet). The SFINAE rule does not apply here, because the ill-formed expression is not in direct context of template argument type, thus it is treated as a hard error.
In order to make the ill-formation happen at an immediate context, use the following code:
#include <type_traits>
template <bool B, class T>
struct lazy_enable_if_c {
typedef typename T::type type;
};
template <class T>
struct lazy_enable_if_c<false, T> {};
template <class Cond, class T>
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
template <class T>
struct type_wrapper {
using type = T;
};
#define REQUIRES(...) std::enable_if_t<(__VA_ARGS__), bool> = true
template<typename Derived>
struct ExpressionBase {
Derived& derived() { return static_cast<Derived&>(*this); }
const Derived& derived() const { return static_cast<const Derived&>(*this); }
struct MyCond {
static constexpr bool value = Derived::size() == 1;
};
template<typename T, typename = typename lazy_enable_if<MyCond, type_wrapper<T>>::type>
operator T () const {
return T{};
}
};
struct Derived : public ExpressionBase<Derived>
{
constexpr static int size() {
return 1;
}
};
int main() {
Derived d;
int i = d;
return 0;
}
It is actually adapted from boost, which you can find more details here.

How to check template class method return type?

I try to check in Class2 return return value type of method getGG() of class given as template parameter but my code doesn't compile. How to do it properly?
template <class T, class U>
struct hasProperMethodReturnValueType {
static constexpr bool value = std::is_same<T, std::decltype(U.getGG())>;
};
template<class P> class Class1 {
private:
P gg;
public:
Class1(P a) : gg(a) {}
P getGG() {
return gg;
}
};
template<class A, class P> class Class3 {
private:
P gg;
A dd;
public:
Class3(P a, A r) : gg(a), dd(r) {}
P getGG() {
return gg;
}
};
template<class G, class R> class Class2 {
static_assert(hasProperMethodReturnValueType<G, R>::value, "Not same type");
private:
R cc;
public:
Class2(R r) : cc(r) {};
};
int main() {
auto obj = Class2<int, Class1<int> >(Class1<int>(3));
auto obj2 = Class2<int, Class3<float, int> >(Class3<float, int>(0, 1.1));
return 0;
}
Compilation error:
error: template argument 2 is invalid
static constexpr bool value = std::is_same<T, std::decltype(U.getGG())>;
use std::declval.
template <class T, class U>
struct hasProperMethodReturnValueType
: std::is_same<T, decltype(std::declval<U>().getGG())>
{};
https://wandbox.org/permlink/iWUCOyssN3sVo2yH
In std::decltype(U.getGG()), U is a type, while getGG is a member function. U.getGG() is simply invalid syntax - you need to "create" an instance of U to call the member function - std::declval is an utility that does that for you in unevaluated contexts. Also std::decltype does not exist - decltype is a keyword.
decltype(std::declval<U>().getGG())

Property system based on std::any: template type deduction

To implement a property system for polymorphic objects, I first declared the following structure:
enum class access_rights_t
{
NONE = 0,
READ = 1 << 0,
WRITE = 1 << 1,
READ_WRITE = READ | WRITE
};
struct property_format
{
type_index type;
string name;
access_rights_t access_rights;
};
So a property is defined with a type, a name and access rights (read-only, write-only or read-write). Then I started the property class as follows:
template<typename Base>
class property : property_format
{
public:
template<typename Derived, typename T>
using get_t = function<T(const Derived&)>;
template<typename Derived, typename T>
using set_t = function<void(Derived&, const T&)>;
private:
get_t<Base, any> get_f;
set_t<Base, any> set_f;
The property is associated to a base type, but may (and will) be filled with accessors associated to an instance of a derived type. The accessors will be encapsulated with functions accessing std::any objects on an instance of type Base. The get and set methods are declared as follows (type checking are not shown here to make the code minimal):
public:
template<typename T>
T get(const Base& b) const
{
return any_cast<T>(this->get_f(b));
}
template<typename T>
void set(Base& b, const T& value_)
{
this->set_f(b, any(value_));
}
Then the constructors (access rights are set to NONE to make the code minimal):
template<typename Derived, typename T>
property(
const string& name_,
get_t<Derived, T> get_,
set_t<Derived, T> set_ = nullptr
):
property_format{
typeid(T),
name_,
access_rights_t::NONE
},
get_f{caller<Derived, T>{get_}},
set_f{caller<Derived, T>{set_}}
{
}
template<typename Derived, typename T>
property(
const string& name_,
set_t<Derived, T> set_
):
property{
name_,
nullptr,
set_
}
{
}
The functions passed as arguments are encapsulated through the helper structure caller:
private:
template<typename Derived, typename T>
struct caller
{
get_t<Derived, T> get_f;
set_t<Derived, T> set_f;
caller(get_t<Derived, T> get_):
get_f{get_}
{
}
caller(set_t<Derived, T> set_):
set_f{set_}
{
}
any operator()(const Base& object_)
{
return any{
this->get_f(
static_cast<const Derived&>(object_)
)
};
}
void operator()(Base& object_, const any& value_)
{
this->set_f(
static_cast<Derived&>(object_),
any_cast<Value>(value_)
);
}
};
Now, considering these dummy classes.
struct foo
{
};
struct bar : foo
{
int i, j;
bar(int i_, int j_):
i{i_},
j{j_}
{
}
int get_i() const {return i;}
void set_i(const int& i_) { this->i = i_; }
};
I can write the following code:
int main()
{
// declare accessors through bar methods
property<foo>::get_t<bar, int> get_i = &bar::get_i;
property<foo>::set_t<bar, int> set_i = &bar::set_i;
// declare a read-write property
property<foo> p_i{"bar_i", get_i, set_i};
// declare a getter through a lambda
property<foo>::get_t<bar, int> get_j = [](const bar& b_){ return b_.j; };
// declare a read-only property
property<foo> p_j{"bar_j", get_j};
// dummy usage
bar b{42, 24};
foo& f = b;
cout << p_i.get<int>(f) << " " << p_j.get<int>(f) << endl;
p_i.set<int>(f, 43);
cout << p_i.get<int>(f) << endl;
}
My problem is that template type deduction doesn't allow me to declare a property directly passing the accessors as arguments, as in:
property<foo> p_i{"bar_i", &bar::get_i, &bar::set_i};
Which produces the following error:
prog.cc:62:5: note: template argument deduction/substitution failed:
prog.cc:149:50: note: mismatched types std::function<void(Type&, const Value&)> and int (bar::*)() const
property<foo> p_i{"bar_i", &bar::get_i, set_i};
Is there a way to address this problem while keeping the code "simple"?
A complete live example is available here.
std::function is a type erasure type. Type erasure types are not suitable for deduction.
template<typename Derived, typename T>
using get_t = function<T(const Derived&)>;
get_t is an alias to a type erasure type. Ditto.
Create traits classes:
template<class T>
struct gettor_traits : std::false_type {};
this will tell you if T is a valid gettor, and if so what its input and output types are. Similarly for settor_traits.
So
template<class T, class Derived>
struct gettor_traits< std::function<T(Derived const&)> >:
std::true_type
{
using return_type = T;
using argument_type = Derived;
};
template<class T, class Derived>
struct gettor_traits< T(Derived::*)() >:
std::true_type
{
using return_type = T;
using argument_type = Derived;
};
etc.
Now we got back to the property ctor:
template<class Gettor,
std::enable_if_t< gettor_traits<Gettor>{}, int> =0,
class T = typename gettor_traits<Gettor>::return_value,
class Derived = typename gettor_traits<Gettor>::argument_type
>
property(
const string& name_,
Gettor get_
):
property_format{
typeid(T),
name_,
access_rights_t::NONE
},
get_f{caller<Derived, T>{get_}},
nullptr
{
}
where we use SFINAE to ensure that our Gettor passes muster, and the traits class to extract the types we care about.
There is going to be lots of work here. But it is write-once work.
My preferred syntax in these cases would be:
std::cout << (f->*p_i)();
and
(f->*p_i)(7);
where the property acts like a member function pointer, or even
(f->*p_i) = 7;
std::cout << (f->*p_i);
where the property transparently acts like a member variable pointer.
In both cases, through overload of ->*, and in the second case via returning a pseudo-reference from ->*.
At the end of this answer is a slightly different approach. I will begin with the general problem though.
The problem is &bar::get_i is a function pointer to a member function while your alias is creating a function object which needs the class as additional template parameter.
Some examples:
Non member function:
#include <functional>
void a(int i) {};
void f(std::function<void(int)> func)
{
}
int main()
{
f(&a);
return 0;
}
This works fine. Now if I change a into a struct:
#include <functional>
struct A
{
void a(int i) {};
};
void f(std::function<void(int)> func)
{
}
int main()
{
f(std::function<void(int)>(&A::a));
return 0;
}
this gets the error:
error: no matching function for call to std::function<void(int)>::function(void (A::*)(int))'
because the std::function object also need the base class (as you do with your alias declaration)
You need a std::function<void(A,int)>
You cannot make your example much better though.
A way to make it a "bit" more easier than your example would maybe be this approach using CRTP.
#include <functional>
template <typename Class>
struct funcPtr
{
template <typename type>
using fun = std::function<void(Class,type)>;
};
struct A : public funcPtr<A>
{
void a(int i) {};
};
void f(A::fun<int> func)
{
};
int main()
{
f(A::fun<int>(&A::a));
return 0;
}
And each your "derived" classes derives from a funcPtr class which "auto generates" the specific alias declaration.

Passing in member variables of a specified class

I want to pass in the name of a member variable. I thought I could do this by
template <typename T::*>
void SetVal(T::* newval)
{
};
This obviously doesn't work, but hopefully gets across what I'm trying to do. I want to be able to set a certain member variable of the templated class.
You can always put compilation-defined constant as template arguments. So here that would be:
template <typename T, typename R, R T::* member>
R& SetVal(T& t, const R& value)
{
t.*member = value;
return t.*member;
}
struct A
{
int a;
};
int main()
{
A a;
SetVal<A,int,&A::a>(a, 10);
return 0;
}