Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does this statement mean in C++ concept and why it cannot works

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  1. 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
  2. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading