eri*_*rip 4 c++ metaprogramming variadic-templates c++11 c++14
我读了这个问题并认为它很有趣,所以我开始玩一些代码来看看我是否可以让它工作,但我遇到了一个问题.
我的方法是使用熟悉函数式编程的头尾习语.但是,我找不到处理空的可变参数模板列表的方法,这将是基本情况.
这是我的代码:
#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
}
Run Code Online (Sandbox Code Playgroud)
我目前收到一个错误'are_convertible' is not a class template,所以我试图转发声明它,这给出了这个错误:
错误:模板参数数量错误(0,应至少为2)
我该如何修复我的方法?
你有两个问题.
首先,使用您的前向声明,您说您的模板始终接受至少两个参数(T1和T2).如果要为结构不允许任何参数,则需要仅使用variadic参数对其进行前向声明:
template<typename... Args>
struct are_convertible;
Run Code Online (Sandbox Code Playgroud)
其次,您的第二个定义不是部分特化,而是与先前的前向声明相矛盾的完整的一般(新的)模板定义.你需要的是部分专业化:
template <typename T1, typename T2, typename... Args>
struct are_convertible<T1, T2, Args...> {
//^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
在此之后,您的代码有效:
class A {};
class B : public A {};
class C {};
class D : public C {};
template<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<T1, T2, Args...> {
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;
std::cout << "Are convertible B->A and D->C: " << are_convertible<B, A, D, C>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这版画false和true,这似乎是对我来说是正确的结果.
| 归档时间: |
|
| 查看次数: |
1075 次 |
| 最近记录: |