特征检测部门的Sfinae问题
SFINAE issue with traits detecting division
i具有以下特征要检测两种类型是否可分开,然后返回结果操作类型或其他Default
类型类型,否则:
struct default_t { };
// Overloaded template for fallbacks
template <class U, class V>
default_t operator/(U, V);
// If the expression std::declval<U>()/std::declval<V>() is valid,
// gives the return type, otherwize provide Default.
template <class U, class V, class Default>
struct div_type_or {
using type_ = decltype(std::declval<U>() / std::declval<V>());
using type = typename std::conditional<
std::is_same<type_, default_t>{},
Default,
type_>::type;
};
template <class... Args>
using div_type_or_t = typename div_type_or<Args...>::type;
这与libstdc 很好,但与libc 无关,当我尝试使用不可分割的std::chrono::duration
类型时,例如:
struct A { };
div_type_or_t<std::chrono::seconds, A, int> b;
我有以下错误:
/library/developer/commandlinetools/usr/includs/c /v1/chrono:764:81: 错误:没有类型为"类型"的类型 'std :: __ 1 :: common_type' typename common_type :: type> :: value> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/library/developer/commandlinetools/usr/include/c /v1/charrono:777:7: 注意:实例化默认参数 '__duration_divide_imp>,a>'此处需要 :__duration_divide_imp,_rep2> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>
/library/developer/commandlinetools/usr/includs/c /v1/charrono:784:10: 注意:在模板类的实例化中 'std :: __ 1 :: chrono :: __ duration_divide_result>,a,false>' 在此处请求TypeName __duration_divide_result,_rep2> ::类型 ^
test.cpp:16:46:注意:将推论模板参数替换为函数模板'operator/'[with _rep1 = long long,_period = std :: __ 1 :: ratio&lt; 1,1>, rep2 = a] 使用type = dectType(std :: dectval()/std :: dectval()); ^
test.cpp:24:1:注意:在模板类的实例化中 'div_type_or>,a,d>'在此请求使用div_type_or_t = typenamediv_type_or :: type;^
test.cpp:48:19: 注意:在模板的实例化中类型的别名'div_type_or_t' 在这里要求 div_type_or_t,d> {},");
据我了解,这是因为std::chrono::duration
的operator/
的以下过载失败:
template< class Rep1, class Period, class Rep2 >
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator/( const duration<Rep1, Period>& d,
const Rep2& s );
我以为std::common_type
是功能签名的一部分,这将允许Sfinae工作并使用我的自定义operator/
的过载,从而推断default_t
,但显然这不起作用...
由于这与libstdc 一起使用,所以我只是想知道我的代码是否存在问题(也许我不明白Sfinae在这种情况下的工作方式),或者这是否是libc bug?
在libc 中,我们有:
template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
struct __duration_divide_result
{
};
template <class _Duration, class _Rep2,
bool = is_convertible<_Rep2,
typename common_type<typename _Duration::rep, _Rep2>::type>::value>
struct __duration_divide_imp
{
};
template <class _Rep1, class _Period, class _Rep2>
struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
{
typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
};
template <class _Rep1, class _Period, class _Rep2>
struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
: __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
{
};
template <class _Rep1, class _Period, class _Rep2>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR
typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{
typedef typename common_type<_Rep1, _Rep2>::type _Cr;
typedef duration<_Cr, _Period> _Cd;
return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
}
所以而不是 std::common_type<_Rep1, _Rep2>::type
,
他们使用__duration_divide_result<duration<_Rep1, _Period>, _Rep2 /*, false*/>
,
实例化__duration_divide_imp<duration<_Rep1, _Period>, _Rep2
/*, is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value*/>
用法是一个困难的错误。
我会说,在这方面,libc 中的实现是不正确的。
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 基于 SFINAE 的特征实现问题与函数模板重载
- 关于如何使用此代码段实现 SFINAE 的问题
- 将 SFINAE 用于模板化构造函数时出现问题
- 有关类模板和功能模板的Sfinae的问题
- 特征检测部门的Sfinae问题
- ADL相关的GCC 4.7.2表达Sfinae的问题
- 我的SFINAE检查std::map/std::vector有什么问题
- is_container trait在std::set SFINAE问题上失败
- 替换失败不是static_cast的错误(SFINAE)问题