6

Is there a way to make the compiler deduce the parameter size_t L using a constrained by concepts template argument?

That's the best I got

template <typename T, size_t L>
concept def_bitset = std::is_same<T, std::bitset<L>>::value;

template<size_t L>
void stamp(def_bitset<L> auto const &b) {
    std::cout << b << std::endl;
}

This works if I call it passing the L as a template argument

stamp<4>(bitset<4>{0b0110});

I'd like to be able to call stamp without repeating the bitset size.

10
  • 1
    Does this compile if you pass a larger L (for example) to stamp?
    – kabanus
    Oct 15, 2020 at 11:53
  • It doesn't, that's a note from the compiler note: the expression 'std::is_same<T, std::bitset<L> >::value [with T = std::bitset<4>; L = 5]' evaluated to 'false' Oct 15, 2020 at 11:55
  • 3
    Now, I apologize in advance if this is just me misunderstanding an overly simplified example, but I don't see why you'd need any C++20 machinery here. stamp can simply be defined to take void stamp(std::bitset<L> const &b) - Tried and tested, simple and readable. Oct 15, 2020 at 11:55
  • @StoryTeller-UnslanderMonica Doesn't it seem odd though that this won't work as well? I imagine more complex concepts are the end goal here.
    – kabanus
    Oct 15, 2020 at 12:00
  • 1
    @kabanus yes, that's still covered by the (admittedly convoluted) rules of function template overloading, specifically the derived-to-base implicit conversions. But the more I think about it, the less I'm sure that the deductions for the argument type and the concept need to occur at the same time rather than in sequence -- it does not surprise me that this is not supported, but I'm not sure how hard it would be.
    – Quentin
    Oct 15, 2020 at 12:28

1 Answer 1

5

Concepts exist to check constraints on template arguments. A concept is not a type, despite appearing in the same location as a type can when employing terse notation. It doesn't carry any type information that can be used for template argument deduction.

To illustrate it, let's rewrite your terse function template definition to its explicit form

template<size_t L, def_bitset<L> T>
void stamp(T const &b) {
    std::cout << b << std::endl;
}

This equivalent form should illustrate the problem. The concept can only check the constraint holds after T and L have been deduced. But while T is easy enough to deduce, this function template definition is not written in a way that allows deducing L from b. Therefore you encountered the need to specify L explicitly. The concept is of no help, because it's not part of the function argument, it's part of the template itself.

Now, it just so happens that the use case in the question doesn't really require a concept at all.

template<size_t L>
void stamp(std::bitset<L> const &b) {
    std::cout << b << std::endl;
}

This is a function template that expects specializations of std::bitset.

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.