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

Concepts: require a function on a type without default constructing that type

I need to require of some type A that there exists a function f(A, A::B).

I’m testing that by calling f with instances of A and A::B. Is there a less ostentatious way to test against an instance of the dependent type without requiring default constructible?

template <class Container>
concept CanPutData = requires (Container a)
{
  //put(typename Container::data_type{}, a);  // overconstrained
  put(*reinterpret_cast<typename Container::data_type*>(0), a); // oof
};

void test(CanPutData auto container) {}

template<class Container>
void put(typename Container::data_type const& data, Container& into) {}


template<class Data>
struct data_container { using data_type = Data; };
struct not_default_constructible_data { int& v; };

int main()
{
  test(data_container<not_default_constructible_data>{});

  return 0;
}

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

>Solution :

Same way you’re already getting a Container without requiring that one to be default constructible: by just sticking it in the parameter list of the requires expression:

template <class Container>
concept CanPutData = requires (Container container, typename Container::data_type data)
{
  put(data, container);
};
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