I read this question and thought it was interesting, so I started playing with some code to see if I could get it to work, but I ran into an issue.
My approach is to use the head-tail idiom familiar to functional programming. However, I could not find a way to deal with an empty variadic template list, which would be the base case.
Here's my code:
#include <iostream>
#include <type_traits>
class A {};
class B : public A {};
class C {};
class D : public C {};
/*
// Forward declaration
template <typename T1, typename T2, typename... Args>
struct are_convertible;
*/
// There are no Args
template <>
struct are_convertible<> {
static const bool value = true;
};
// Check if the first two elements are convertible then recurse on the rest
template <typename T1, typename T2, typename... Args>
struct are_convertible {
static const bool value = std::is_convertible<T1, T2>::value && are_convertible<Args...>::value;
};
int main() {
std::cout << std::boolalpha;
std::cout << "Are convertible A->B and C->D: " << are_convertible<A, B, C, D>::value << std::endl; // Should be false
}
I'm currently getting an error that states 'are_convertible' is not a class template, so I tried to forward declare it, which gave this error:
error: wrong number of template arguments (0, should be at least 2)
Is my approach wrong, is this impossible, or is a combination of both?
Aucun commentaire:
Enregistrer un commentaire