TS Concepts - 括号概念的优点

时间:2017-11-16 12:29:00

标签: c++ c++-concepts c++-ts

有人可以解释我为什么这两个(稍微)不同的代码片段同样工作?
是否使用其中一个的优势?

#1单程

template<typename T>
concept bool Swappable = requires (T a, T b) {
    {std::swap(a, b)} -> void;
};

#2其他方式

template<typename T>
concept bool Swappable() {
    return requires(T a, T b) {
        {std::swap(a, b)} -> void;
    };
};

第二个示例使用括号可交换并返回“要求”的原因是什么?

可能主要:

int main() {
    static_assert(Swappable<int>, "NOT SWAPPABLE!"); // Compiles when #1
    // static_assert(Swappable<int>(), "NOT SWAPPABLE!"); // Compiles when #2

    return 0;
}

0 个答案:

没有答案
相关问题