samedi 11 avril 2020

Why C++ Concepts return type requirement be a type-constraint and not also Primitive type?

Hello everyone Concepts are now a part of C++20 standard and to play with it I basically wrote this example that I saw sometime back

#include<iostream>

template<typename T>
concept Container= requires (T a){

    { a.size() } -> size_t;
    // others
}

template<Container Holder>
class Demo{};
int main(){


    Demo<std::vector<int>> d;


}

But on compiling the code I got the following error

error: return-type-requirement is not a type-constraint ( { a.size() } -> size_t;)

Later on i remembered that return type constraint cannot be a primitive type and changed the code with std::same_as

#include<iostream>

template<typename T>
concept Container= requires (T a){

    { a.size() } -> std::same_as<size_t>;
    // others
}

template<Container Holder>
class Demo{};
int main(){

    Demo<std::vector<int>> d;

}

where std::same_as is itself a concept.

But why cant primitive types be used? I know that it breaks something in C++ but i am not able to remember it.

Aucun commentaire:

Enregistrer un commentaire