I’m trying C++20 features, and I want to use concept to restrain the template to pass only the class with a member named List and its type should be std::vector<int> . Here is the foreword.
To do this without using concept, I will write this code:
class RandomClass
{
public:
std::vector<int> List;
};
typename std::enable_if<std::is_same<decltype(T::List), std::vector<int>>::value>::type
func(T& arg)
{
...
}
int main()
{
auto randomObj= RandomClass();
func(randomObj);
}
I asked New Bing to use concept to rewrite it, and it gave this:
template<class T>
concept HasList = requires(T t) {
{ t.List } -> std::same_as<std::vector<int>>;
};
template<class T> requires HasList<T>
void func(T& arg)
But this code cannot be compiled, and the compiler gave 'func': no matching overloaded function found .
I then search std::same_as usage and tried this:
template<class T>
concept HasList = requires(T t) {
std::same_as<AddressListType, decltype(T::List)>;
};
This code worked.
Now here is the questions:
- What does the symbol in these statement
{ t.List } -> std::same_as<std::vector<int>>;mean? I have no idea where this{}and->come from - Why
{ t.List } -> std::same_as<std::vector<int>>;cannot work?
>Solution :
There’s just one small issue with your concept: The member variable is considered a reference to a vector. Using the following version of the concept should work:
template<class T>
concept HasList = requires(T t) {
{ t.List } -> std::same_as<std::vector<int>&>;
};
In
{ t.List } -> std::same_as<std::vector<int>>;
t.List is an expression and std::same_as<std::vector<int>> is a concept that must be satisfied. specifically
std::same_as<X, std::vector<int>>
must be satisfied for X being the type of the expression t.List.