相关疑难解决方法(0)

MSVC std :: pair实现:SFINAE在这里正确应用了吗?

请考虑以下Microsoft Visual Studio 15.4.5附带std::pairSTL实现中的默认构造函数代码:

template<class _Uty1 = _Ty1,
    class _Uty2 = _Ty2,
    class = enable_if_t<is_default_constructible<_Uty1>::value
                    && is_default_constructible<_Uty2>::value>>
    constexpr pair()
    : first(), second()
    {   // default construct
    }
Run Code Online (Sandbox Code Playgroud)

我设置/std:c++latest选项,所以,根据标准(我在这里使用草案n4659)我期望这个构造函数将被排除在重载决议之外,如果其中一个_Ty1_Ty1不是默认构造:

23.4.2类模板对[pairs.pair]

EXPLICIT constexpr pair();

效果:值初始化第一个和第二个.

备注:除非is_default_constructible_v<first_type>为true且 is_default_constructible_v<second_type>为true,否则此构造函数不应参与重载决策.[注意:此行为可以通过带有默认模板参数的构造函数模板实现.

在上面的实现中,排除执行如下:

class = enable_if_t<is_default_constructible<_Uty1>::value
                    && is_default_constructible<_Uty2>::value>
Run Code Online (Sandbox Code Playgroud)

据我所知,SFINAE 不适用于模板类型参数默认值.

有趣的是,在Microsoft Visual Studio 15.5.3中,构造函数已更改为"正确版本"(基于我有限的模板知识,"正确"):

template<class _Uty1 = _Ty1,
    class _Uty2 = _Ty2,
    enable_if_t<conjunction_v<
        is_default_constructible<_Uty1>,
        is_default_constructible<_Uty2>
    >, …
Run Code Online (Sandbox Code Playgroud)

c++ templates stl

4
推荐指数
1
解决办法
123
查看次数

SFINAE构造师

我一直喜欢这样的SFINAE语法功能,似乎一般都运行良好!

template <class Integer, class = typename std::enable_if<std::is_integral<Integer>::value>::type>
T(Integer n) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是当我想在同一个班级做同样的事情时,我遇到了一个问题......

template <class Float, class = typename std::enable_if<std::is_floating_point<Float>::value>::type>
T(Float n) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

得到如下错误:

./../T.h:286:2: error: constructor cannot be redeclared
        T(Float n) {
        ^
./../T.h:281:2: note: previous definition is here
        T(Integer n) {
        ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

这些构造函数不应只存在于适当的类型而且不能同时存在吗?他们为什么会有冲突?

我在这里有点厚吗?

另一方面,这确实有效(但我不喜欢语法):

template <class Integer>
T(Integer n, typename std::enable_if<std::is_integral<Integer>::value>::type* = nullptr) {
}

template <class Float>
T(Float n, typename std::enable_if<std::is_floating_point<Float>::value>::type* = nullptr) {
}
Run Code Online (Sandbox Code Playgroud)

c++ sfinae

4
推荐指数
1
解决办法
287
查看次数

SFINAE没有使用一个非常简单的例子

template<typename T, typename = std::enable_if_t<std::is_trivially_copyable<T>::value>>
inline
int test_sfinae(T tc) {
    return 1;
}

template<typename T, typename = std::enable_if_t<!std::is_trivially_copyable<T>::value>>
inline
int test_sfinae(T ntc) {
    return 2;
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么这段代码无法编译并提供此错误消息:

C2995'int test_sfinae(T)':函数模板已经定义

我正在使用MVSC.

c++ templates sfinae

1
推荐指数
1
解决办法
169
查看次数

`typename = enable_if_t&lt;...&gt;` 和 `enable_if_t&lt;...,bool&gt; = true` 之间的区别

使用typename = enable_if_t<...>enable_if_t<...,bool> = true用于 SFINAE之间有什么区别吗?我特别问,因为我偶然发现了一个错误:编译器错误,在 enable_if_t 中有一个折叠表达式

所以我很好奇两者之间是否有任何实际差异。

c++ templates enable-if

1
推荐指数
1
解决办法
144
查看次数

标签 统计

c++ ×4

templates ×3

sfinae ×2

enable-if ×1

stl ×1