gpt4 book ai didi

c++ - 概念可能会取代 C++ 中的类型特征吗?

转载 作者:行者123 更新时间:2023-12-04 11:04:04 37 4
gpt4 key购买 nike

关闭。这个问题需要details or clarity .它目前不接受答案。












想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题.

3个月前关闭。




Improve this question




我已经知道这个概念是一个编译时谓词,它可以约束模板或 auto .
我发现这些概念将返回 bool 类型的纯右值。 ,所以我可以打印它来测试自己。
我也阅读了这个特定问题 Can concepts(C++20) be used as a boolean? 以及澄清可用作 bool 值的概念的答案。

[temp.names](8) A concept-id is a simple-template-id where the template-name is a concept-name. A concept-id is a prvalue of type bool, and does not name a template specialization. A concept-id evaluates to true if the concept's normalized constraint-expression is satisfied ([temp.constr.constr]) by the specified template arguments and false otherwise.


例如:
template <typename T>
concept meowable = requires (T obj) {
{ obj.meow() } -> std::same_as<T&>;
};

struct Dog {};
struct Cat { Cat& meow(); };
struct Wolf { Wolf& woof(); };
应用:
std::cout << std::boolalpha;

std::cout << "Has meow?" << '\n'
<< "Dog: " << meowable<Dog> << '\n'
<< "Cat: " << meowable<Cat> << '\n'
<< "Wolf: " << meowable<Wolf> << '\n';
输出:
Has meow?
Dog: false
Cat: true
Wolf: false
我还可以根据概念创建类型特征:
template <typename T>
struct has_meow : std::bool_constant<meowable<T>> {};

template <typename T>
constexpr bool has_meow_v = has_meow<T>::value;
应用:
std::cout << "Has meow?" << '\n'
<< "Dog: " << has_meow_v<Dog> << '\n'
<< "Cat: " << has_meow_v<Cat> << '\n'
<< "Wolf: " << has_meow_v<Wolf> << '\n';
其输出与上面相同。
根据我对类型特征的了解,它们是能够检查类型的“属性”的技术,其中包括编译时谓词(不是概念,例如: is_voidis_bounded_array )。如果我没记错的话,它们现在是“反射”的一部分。

我的主要问题是:概念可能会取代类型特征吗?

这似乎是一个循环:概念使用类型特征,而类型特征可能使用概念。
  • 概念是否有助于类型特征在模板元编程的世界中扩展?
  • 我什么时候应用这些概念或 requires表达式作为“帮助者”/“有能力”在“误用”时以相同的方式键入特征 std::enable_if做了,也许没有用作约束。 (如果它被误用或间接应用于语言对我来说很好)

  • 注意:我不问概念如何以正确的方式使用,但我试图问概念是否可能在编译时谓词方面取代类型特征。

    最佳答案

    (注意:这个答案是在 OP 开始更新和扩展原始问题之前写的)

    When do I apply these kinds of stuff in C++?


    当在 requires 子句的约束逻辑或表达式中使用概念时,概念 id 是 bool 类型的纯右值这一事实是必不可少的:
    template<...>
    void f() requires .... {}
    // ^^^^ - constraint-logical-or-expression
    //
    // ^^^^^^^^^^^^^ - requires-clause
    其中,特别是根据 [temp.pre]/1 ,requires 子句的语法是:
    requires-clause:
    requires constraint-logical-or-expression

    反过来,约束逻辑或表达式是 primary-expression ,其中包括 requires-expression:s .
    例如。如以下(人为的)示例:
    #include <iostream>
    #include <type_traits>

    template <typename T>
    concept has_static_meow = requires (T obj) {
    { decltype(obj)::meow() } -> std::same_as<void>;
    };

    template <typename T>
    concept has_static_bark = requires (T obj) {
    { decltype(obj)::bark() } -> std::same_as<void>;
    };

    struct Dog { static void bark() { std::cout << "bark\n"; }};
    struct Cat { static void meow() { std::cout << "meow\n"; } };

    template<typename T>
    struct MakeNoise {
    static void bark() requires has_static_bark<T> { T::bark(); }
    static void meow() requires has_static_meow<T> { T::meow(); }
    };

    int main() {
    MakeNoise<Dog>::bark();
    // MakeNoise<Dog>::meow(); // error with a nice error message
    MakeNoise<Cat>::meow();
    // MakeNoise<Cat>::bark(); // error with a nice error message
    }

    关于c++ - 概念可能会取代 C++ 中的类型特征吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67804691/

    37 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com