0

Continuing with my studies on C++ 20 concepts, I am trying to write a concept that would assure that every type in a std::tuple has inner type defined with a specific name, and that all the inners have the same type.

I tried this:

#include <concepts>
#include <string>
#include <tuple>

template <typename t>
concept type_model = requires(t p_t) {
  typename t::inner;
};

template <typename t, typename u>
concept same_inner = requires(t p_t, u p_u) {
  std::is_same_v<typename t::inner, typename u::inner>;
};

template <typename t, typename... t_type_model>
concept container_model = requires(t p_t) {

  requires((same_inner<t_type_model,
                       std::tuple_element_t<0, std::tuple<t_type_model...>>> &&
            ...));
};

struct type_1 {
  using inner = int;
};

struct type_2 {
  using inner = std::string;
};

template <type_model... t_type_models> struct container {};

template <type_model... t_type_model>
void f(container_model<t_type_model...> auto) {}

using containers_X = container<type_1, type_2>;

int main() {

  containers_X _x;

  f(_x);

  return 0;
}

I expected an error of concept not satisfied because type_1::inner and type_2::inner have different types, but no errors were reported.

4 Answers 4

2

This code,

template <typename t, typename... t_type_model>
concept container_model = requires(t p_t) {

  requires((same_inner<t_type_model,
                       std::tuple_element_t<0, std::tuple<t_type_model...>>> &&
            ...));
};

does not actually test the condition within the second requires clause.

The correct formulation is

template <typename t, typename u>
concept same_inner = std::is_same_v<typename t::inner, typename u::inner>;

or,

template <typename t, typename u>
concept same_inner = requires (t i, u j) {
    requires (std::same_as<typename t::inner, typename u::inner>);
};

This has tripped me up in the past.

Update

The container_model concept

template <typename t, typename... t_type_model>
concept container_model = requires(t p_t) {

  requires((same_inner<t_type_model,
                       std::tuple_element_t<0, std::tuple<t_type_model...>>> &&
            ...));
};

need to be something like

template <typename t, typename... t_type_model>
concept container_model = (same_inner<t, t_type_model> && ...);

At this point, the following assertions should hold:

static_assert(not same_inner<type_1, type_2>);
static_assert(not container_model<type_1, type_2>);

When the function f is invoked, it is passed container<type_1, type_2> which qualifies as a container_model because it gets translated to container_model<container<type_t, type_2>> which returns true.

One possible fix is to change f to something like

template <type_model... t_type_model>
void f(container_model<container<t_type_model...>> auto) {}

Summary

Just to be clear, here is all the code with changes and this code fails to compile because the container_model constraints are not satisfied.

#include <concepts>
#include <string>
#include <tuple>

template <typename t>
concept type_model = requires(t p_t) {
  typename t::inner;
};

template <typename t, typename u>
concept same_inner = std::is_same_v<typename std::decay_t<t>::inner,
                                    typename std::decay_t<u>::inner>;

template <typename t, typename... t_type_model>
concept container_model = (same_inner<t, t_type_model> && ...);

struct type_1 {
  using inner = int;
};

struct type_2 {
  using inner = std::string;
};

template <type_model... t_type_models> struct container {};

template <type_model... t_type_model>
void f(container_model<container<t_type_model...>> auto) {}

using containers_X = container<type_1, type_2>;

static_assert(not same_inner<type_1, type_2>);
static_assert(not container_model<type_1, type_2>);

int main() {

  containers_X _x;

  f(_x);

  return 0;
}

New Update

The previous update was incomplete at best. Here is the latest code which I believe enforces the desired concepts.

Sample Code

#include <concepts>
#include <string>
#include <tuple>

// Does T have an inner type named `inner`.
template<class T>
concept Inner = requires(T x) {
    typename T::inner;
};

// Do T and U have same inner type.
template<class T, class U>
concept SameInner = Inner<T> and Inner<U> and std::is_same_v<typename T::inner, typename U::inner>;

// Helper for checking that all template parameters satisfy Inner
// concept and all pairs satisfy SameInner.
template<typename>
struct inner_container_impl : std::false_type {};

template<template<typename...> class Tp, Inner T>
struct inner_container_impl<Tp<T>> {
    static constexpr bool value = true;
};

template<template<typename...> class Tp, Inner T, Inner... Ts>
struct inner_container_impl<Tp<T, Ts...>> {
    static constexpr bool value = (SameInner<T, Ts> and ...);
};

// The concept just use the helper.
template<class T>
concept InnerContainer = inner_container_impl<T>::value;

// Test types.
struct type_1 {
    using inner = int;
};

struct type_2 {
    using inner = std::string;
};

struct type_3 {
    using inner = int;
};

template<Inner... Ts> struct container {};

void f(InnerContainer auto) {}

using containers_X = std::tuple<type_1, type_2>;
using containers_Y = std::tuple<type_1, type_3>;

static_assert(not SameInner<type_1, type_2>);
static_assert(not InnerContainer<std::tuple<type_1, type_2>>);
static_assert(not InnerContainer<containers_X>);

int main() {

    containers_X _x;
    f(_x);

    containers_Y _y;
    f(_y);

  return 0;
}

Output

/work/so/scratch/src/p4.cpp:64:5: error: no matching
      function for call to 'f'
    f(_x);
    ^
/work/so/scratch/src/p4.cpp:52:6: note: candidate
      template ignored: constraints not satisfied [with auto:1 = std::tuple<type_1,
      type_2>]
void f(InnerContainer auto) {}
     ^
/work/so/scratch/src/p4.cpp:52:8: note: because
      'std::tuple<type_1, type_2>' does not satisfy 'InnerContainer'
void f(InnerContainer auto) {}
       ^
/work/so/scratch/src/p4.cpp:35:26: note: because
      'inner_container_impl<tuple<type_1, type_2> >::value' evaluated to false
concept InnerContainer = inner_container_impl<T>::value;
                         ^
1 error generated.
6
  • I tried both forms, but the compile is still not reporting any errors, Maybe there is something wrong in container_model?
    – canellas
    May 11 at 3:31
  • Yes, I see. There are two moe issues. I will update my answer.
    – RandomBits
    May 11 at 3:45
  • I see what is wrong, but I would like to avoid having to use container_model<container<t_type_model...>>. I wonder if there is a way to define container_model so that I could pass to f a std::tuple<type_1, type_2>. I mean, container_model would exist just to make sure that all the types in a std::tuple satisfy same_inner.
    – canellas
    May 11 at 12:13
  • I changed to using containers_X = container<type_1, type_1>; and the following errors were reported: line 39: No matching function for call to 'f' line 28: candidate template ignored: constraints not satisfied [with t_type_model = <>, auto:2 = containers_X] line 28: because 'container_model<container<type_1, type_1>, container<> >' evaluated to false line 15: because 'same_inner<container<type_1, type_1>, container<> >' evaluated to false line 11: because substituted constraint expression is ill-formed: no type named 'inner' in 'container<type_1, type_1>'
    – canellas
    May 11 at 12:37
  • That update was clearly incorrect on my part -- I guess it was too late for me to be writing code. I added another update -- I rewrote the code more in my style so that I could understand it better -- you should be able to translate it back pretty easily. I think this latest version enforces the concepts as you intended.
    – RandomBits
    May 11 at 13:27
2

Your concept same_inner have no effect and can be compiled with any types.

You should use same_as concept in your same_inner concept restriction, instead of is_same, which always compiles successfully and you cound fetch the result from.

template<typename T, typename U>
concept same_inner = same_as<typename T::inner, typename U::inner>;

This may fix the problem.

See here for detailed informations.

2
  • I believe the correct code is template<typename T, typename U> concept same_inner = same_as<typename T::inner, typename U::inner>;, but still no errors were reported by the compiler.
    – canellas
    May 11 at 3:33
  • @canellas You are right, my apology. Omitting typename is a Microsoft Extension, not in cpp standard. Answer edited.
    – Gorun
    May 11 at 5:12
0

Besides @RandomBits implementation, I believe I found another one:

#include <concepts>
#include <iomanip>
#include <iostream>
#include <string>
#include <tuple>

template <typename t>
concept has_inner = requires(t p_t) {
  typename t::inner;
};

template <has_inner t_1, has_inner t_2> struct same_inner {
  static constexpr bool yes{
      std::is_same_v<typename t_1::inner, typename t_2::inner>};
};

template <typename t_tuple, size_t t_idx = std::tuple_size_v<t_tuple> - 1>
struct check_if_all_has_inner {
  static constexpr bool yes{
      same_inner<typename std::tuple_element_t<t_idx, t_tuple>,
                 typename std::tuple_element_t<t_idx - 1, t_tuple>>::yes &&
      check_if_all_has_inner<t_tuple, t_idx - 1>::yes};
};

template <typename t_tuple> struct check_if_all_has_inner<t_tuple, 0> {
  static constexpr bool yes{true};
};

template <typename t_tuple>
concept container_model = (std::tuple_size_v<t_tuple> > 0) &&
                          (check_if_all_has_inner<t_tuple>::yes);

struct type_1 {
  using inner = int;
};

struct type_2 {
  using inner = std::string;
};

struct type_3 {
  using inner = std::string;
};

struct type_4 {};

void f(container_model auto) {}

int main() {

  /// ERROR 1 if code below uncommented
  //  f(std::tuple<type_1, type_2>{});

  f(std::tuple<type_2, type_3>{});

  {
    std::cout << std::boolalpha
              << check_if_all_has_inner<std::tuple<type_1, type_2>>::yes
              << std::endl;
  }

  {
    std::cout << std::boolalpha
              << check_if_all_has_inner<std::tuple<type_3, type_2>>::yes
              << std::endl;
  }

  ///   ERROR 2 if code below uncommented
  //  {
  //    std::cout << std::boolalpha
  //              << check_if_all_has_inner<std::tuple<type_3, type_2,
  //              type_4>>::yes
  //              << std::endl;
  //  }

  return 0;
}

Where ERROR 1 is

main.cpp:52:3: No matching function for call to 'f'
main.cpp:47:6: candidate template ignored: constraints not satisfied [with
auto:1 = std::tuple<type_1, type_2>] main.cpp:47:8: because 'std::tuple<type_1,
type_2>' does not satisfy 'container_model' main.cpp:31:28: because
'check_if_all_has_inner<tuple<type_1, type_2> >::yes' evaluated to false

and ERROR 2 is

main.cpp:71:78: In instantiation of static data member
'check_if_all_has_inner<std::tuple<type_3, type_2, type_4>, 2>::yes' requested
here main.cpp:20:7: In instantiation of static data member
'check_if_all_has_inner<std::tuple<type_3, type_2, type_4>, 2>::yes' requested
here

main.cpp:20:7: Constraints not satisfied for class template 'same_inner' [with
t_1 = type_4, t_2 = type_2] main.cpp:71:78: in instantiation of static data
member 'check_if_all_has_inner<std::tuple<type_3, type_2, type_4>, 2>::yes'
requested here main.cpp:12:11: because 'type_4' does not satisfy 'has_inner'
main.cpp:9:15: because 'typename t::inner' would be invalid: no type named
'inner' in 'type_4'
0

RandomBits, based on this topic, I think I came up with another implentation:

#include <concepts>
#include <iomanip>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>

// checking if a type is a std::tuple
template <class t_tuple, std::size_t t_idx>
concept has_tuple_element = requires(t_tuple t) {
  typename std::tuple_element_t<t_idx, std::remove_const_t<t_tuple>>;
  {
    get<t_idx>(t)
    } -> std::convertible_to<const std::tuple_element_t<t_idx, t_tuple> &>;
};

template <class t_tuple>
concept tuple_like = !std::is_reference_v<t_tuple> && requires(t_tuple t) {
  typename std::tuple_size<t_tuple>::type;
  requires std::derived_from<
      std::tuple_size<t_tuple>,
      std::integral_constant<std::size_t, std::tuple_size_v<t_tuple>>>;
}
&&[]<std::size_t... t_idx>(std::index_sequence<t_idx...>) {
  return (has_tuple_element<t_tuple, t_idx> && ...);
}
(std::make_index_sequence<std::tuple_size_v<t_tuple>>());

// checking if a type in a index of a tuple has 'inner' type, if the index
// before it has a 'inner' type, and if both 'inner' types are identical
template <size_t t_idx, typename t_tuple> struct same_inner {
  static constexpr bool yes{
      std::is_same_v<typename std::tuple_element_t<t_idx, t_tuple>::inner,
                     typename std::tuple_element_t<t_idx - 1, t_tuple>::inner>};
};

template <typename t_tuple> struct same_inner<0, t_tuple> {
  static constexpr bool yes{true};
};

// std::tuple where all the types have a 'inner' type
template <typename t_tuple>
concept container_model = (tuple_like<t_tuple>)&&[]<std::size_t... t_idx>(
    std::index_sequence<t_idx...>) {
  return (same_inner<t_idx, t_tuple>::yes && ...);
}
(std::make_index_sequence<std::tuple_size_v<t_tuple>>());

// examples
struct type_1 {
  using inner = int;
};

struct type_2 {
  using inner = std::string;
};

struct type_3 {
  using inner = std::string;
};

struct type_4 {};

// function that will handle a std::tuple where all the types have a 'inner'
// type
void f(container_model auto) {}

int main() {

  f(std::tuple<type_2, type_3>{});

  /// ERROR if code below uncommented
  //  f(std::tuple<type_1, type_2>{});

  /// ERROR if code below uncommented
  //   f(std::tuple<type_2, type_4>{});

  /// ERROR if code below uncommented
  //  f(int{3});

  return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.