【发布时间】:2021-02-09 02:52:55
【问题描述】:
在此之前,我想告诉你,我已经尝试自己实现is_assignable。无需再给我展示其他示例 - 我已经看到了一些实现。
感谢您(当然,如果可能的话),我想修复我的解决方案。
所以,这是我的代码:
#include <iostream>
#include <type_traits>
#include <utility>
template<typename LambdaT>
struct is_valid_construction {
is_valid_construction(LambdaT) {}
typedef typename LambdaT lambda_prototype;
template<typename ValueTypeT, typename ExprTypeT = decltype(std::declval<lambda_prototype>()(std::declval<ValueTypeT>()))>
struct evaluate {
evaluate(ValueTypeT val) {
std::cout << "Right!";
}
typedef typename std::true_type value;
};
template<typename ValueTypeT> //The compiler ignores this definition
struct evaluate<ValueTypeT, decltype(std::declval<lambda_prototype>()(std::declval<int>()))> {
evaluate(ValueTypeT val) {
std::cout << "Nope";
}
typedef typename std::false_type value;
};
template<typename ValueTypeT>
void print_value(ValueTypeT val) {
evaluate evaluation(val);
}
};
struct ForTest {};
int main() {
is_valid_construction is_assignable([](auto x) -> decltype(x = x) { });
is_valid_construction is_less_comparable([](auto x) -> decltype(x < x) {});
is_valid_construction is_more_comparable([](auto x) -> decltype(x > x) {});
is_assignable.print_value(int{});
is_less_comparable.print_value(char{});
is_more_comparable.print_value(ForTest{});
return 0;
}
如您所见,我正在尝试在模板结构中定义模板结构。所以,我例外的是,如果 调用(使用declval)这个带有这种类型参数的 lambda 表达式(粗略地说,在替换方面)失败,那么SFINAE 更进一步,应该看到第二个模板定义可以方便实例化。我在问如何修复我的模板结构及其默认参数以推送 SFINAE 使用第二个定义?
【问题讨论】:
标签: c++ templates metaprogramming sfinae