1,362 questions
Advice
1
vote
1
replies
90
views
Constraints in the first template parameter
Why can't we put a constraint on the first type parameter of the concept?
Why can't we use the short form of the concept for non-type parameter?
Why is it possible to specialize concepts only in ...
2
votes
1
answer
182
views
Determine if a Type is formattable with given Format String at compile time
I am trying to come up with a way to determine, whether a given format string is valid for a given Type at compile time.
I was expecting for a simple concept to work:
template<typename T>
...
3
votes
0
answers
118
views
Out of class definition of constrained member function
Having issues finding a syntax for hoisting constrained member function outside of its class that GCC is happy with. At this point I'm starting to think it's a GCC bug.
struct S {
template<...
3
votes
2
answers
185
views
Problem with satisfying a C++ concept in a std::visit
I've a C++ concept where I need to check that the class has a particular public attribute.
My problem is that the concept works if I use it directly, but fails if I use it in std::visit.
This is the ...
2
votes
1
answer
140
views
How to apply a concept to a trailing return type
Here is some basic example:
template <typename T, typename Callable>
constexpr auto foo(T arg, Callable&& make)
-> decltype(make(std::declval<T>())) {
return make(arg);
}...
7
votes
2
answers
222
views
How can I make a concept that accepts only vec3 types and exclude all other types?
I have a function that should only accept 3-component vector types. I have:
template <typename T>
concept Vec3Like = requires(T t) { t.x; t.y; t.z; };
template <Vec3Like vec3_t, ...
6
votes
0
answers
234
views
C++ Concept constraint requirement is not strict
Given the concept,
template <typename T>
concept CanFoo = requires(Eigen::Array2d const& x) {
{ T::Foo(x) } -> std::same_as<double>;
};
I expect, as someone new to actually ...
2
votes
2
answers
111
views
How to write a concept for a move input iterator?
I want to write a function that moves the data of a container into another container using an iterator pair.
I have the following concept:
template <typename Iter, typename T>
concept ...
22
votes
3
answers
2k
views
In a concept, how can I check that a member is static?
This is similar to Understanding concepts. Check if a member is static, but that Q&A only asks why it doesn't work, and here I'm asking how to fix it.
Consider the following code:
struct A
{
...
4
votes
2
answers
182
views
Define a concept in C++ for specific members
I have several structs in the form
struct MyStruct1 {
std::string myName;
// ...
}
but not all of them have a member of type std::string and variable named myName.
I'm writing a function template,...
15
votes
1
answer
403
views
Idiomatic way to define type constraint on forwarding reference argument
When using a type constraint on a forwarding reference argument, the constraint is given as an lvalue reference to the type. For example, the call to h in the following code does not compile because ...
2
votes
0
answers
95
views
Concept constraints for outer definitions of methods of a class with template template parameter
Usually I define my template classes as following (template<template<class> class T> is essential here):
// Foo.h
template<template<class> class T>
class Foo {
void foo();
...
5
votes
1
answer
168
views
Are constraints in C++ std::ranges::ref_view constructor redundant, and is the forwarding reference necessary?
The libstdc++ implementation of std::ranges::ref_view, following the C++ standard, includes the following code:
template<range _Range> requires is_object_v<_Range>
class ref_view : ...
4
votes
1
answer
181
views
What does a universal reference in a requires expression actually mean?
This is an example of a concept from cppreference:
template<class T, class U = T>
concept Swappable = requires(T&& t, U&& u)
{
swap(std::forward<T>(t), std::forward<...
4
votes
2
answers
193
views
Why does MSVC compile this?
Both Clang and GCC refuse to compile this but MSVC doesn't.
class Foo {
int name;
};
template <class T>
concept HasName = requires { T::name; };
static_assert(HasName<Foo>);
MSVC ...
16
votes
1
answer
707
views
How does overload resolution select from multiple prospective destructors?
After seeing this post: If a class has a destructor declared with a requires clause that evaluates to false, why is the class not trivially destructible? I have a separate question, and I haven't ...
2
votes
2
answers
135
views
Deduce maximum number of function arguments (fails with concepts)
I am writing a function probeMaxArgs that deduces the maximum number of arguments in a function/callable object. For simplicity I assume, that the object don't have any overloads of operator() (...
15
votes
1
answer
335
views
Why does the parameter pack not work as expected in concepts?
template<typename T, typename... Args>
inline constexpr auto c = sizeof(T) + sizeof...(Args) > 1;
template<typename... Args>
requires c<Args...> // ok
void f1() {
}
template<...
5
votes
0
answers
213
views
Code using constrained auto rejected by GCC but not by Clang
I am not sure which compiler is right here, as I am not fully proficient with C++20 concepts yet. The following code is rejected by GCC (15.1) saying that there is no matching declaration for Foo::...
4
votes
2
answers
162
views
How to define C++ concept to describe the member function template
For example: define a C++ concept with the following constraint: the type should have a function template called f that receive an integer value as the parameter.
Here is a valid type:
struct A
{
...
7
votes
1
answer
148
views
Out-of-class definition with requires clause is rejected by clang but works with gcc
Clang currently rejects the following code:
template <typename X>
constexpr bool is_valid = true;
template<typename T>
class Nesting
{
public:
template<typename Q> requires ...
2
votes
2
answers
110
views
Using std::apply in a concept causes compilation error
I'm trying to test if a function overload exists for a list of types. Arguments for the function are bundled in a tuple, therefore I used std::apply in the testing concept. To my surprise, the concept ...
19
votes
2
answers
1k
views
Why are disabled overloads required to be unique?
The following class has four overloads of function f. When T!=int, all overloads have unique parameter lists, but when T=int, all overloads have the same parameter lists. To make it compile even when ...
0
votes
0
answers
107
views
Why does std::equality_comparable_with not work in the simplest case? [duplicate]
#include <concepts>
struct A {};
struct B {};
constexpr bool operator==(A, A) {
return true;
}
constexpr bool operator==(B, B) {
return true;
}
constexpr bool operator==(A, B) {
...
3
votes
0
answers
126
views
Why does concept satisfaction behave differently for fundamental types vs. user defined types?
In the following code, the line bar(123) fails to compile.
Why does struct A satisfy the CanInvokeFoo concept, whereas int does not satisfy the CanInvokeFoo concept?
struct A {};
template <...
11
votes
1
answer
565
views
Is this concept satisfaction a bug in GCC or did I invoke undefined behavior?
Here's the test code:
#include <concepts>
template<typename T>
concept Printable = requires(T obj) {
{ obj.print() } -> std::same_as<void>;
};
template<Printable T>
...
4
votes
1
answer
286
views
std::apply to a custom, tuple-like type
This problem was encountered while upgrading a C++17 codebase to C++23. The code size is significant (100k+), it is rather technical and quite template-heavy.
On a rather central location, we use std::...
1
vote
0
answers
73
views
Nominal typing with C++20 concepts [duplicate]
I'm trying to replace virtual base classes with concepts in a few places in my codebase where dynamic polymorphism isn't actually being used. However I still want it to be nominally typed. In ...
3
votes
1
answer
152
views
Why add redundant `std::constructible_from<T>` to the concept `std::default_initializable`?
At the cppref page on the concept std::default_initializable, I saw the following code:
template<class T>
concept default_initializable =
std::constructible_from<T> && ...
2
votes
1
answer
189
views
Function call is ambiguous, despite constraint
I'm encountering an ambiguous call error when trying to use a C++20 concept-constrained function alongside a generic template overload. My expectation was that the concept-constrained version would be ...
4
votes
1
answer
226
views
Why do C++20 concepts (requirement expression) using std::is_arithmetic_v fail to enforce type constraints?
I encountered unexpected behavior while implementing C++20 concepts based on an example from "Template Metaprogramming with C++: Learn everything about C++ templates and unlock the power of ...
5
votes
2
answers
184
views
How to assert at compile time that a consteval function cannot be evaluated?
I am writing a function that for the purpose of this question is similar to std::format, in that std::format("{}") fails at compile-time, while std::format("{}", 1) compiles.
I ...
7
votes
1
answer
284
views
What is this "template(bool Other)( requires ....) classname(...)" syntax in the ranges-v3 library?
I was looking at how concat_view was implemented in ranges-v3 and stumbled upon some seemingly unknown syntax for a templated constructor:
template<bool IsConst>
struct sentinel
{
private:
...
0
votes
2
answers
185
views
Why does std::contiguous_iterator require the value type to be a reference?
From cppreference:
template< class I >
concept contiguous_iterator =
std::random_access_iterator<I> &&
std::derived_from</*ITER_CONCEPT*/<I>, std::...
-1
votes
2
answers
101
views
How do I make a 'requires' expression always fail? [closed]
Sometimes you do static_assert(false);, which will always make the compilation fail. I want to do the same with a requires expression, but I really don't know how to do it.
Also, could someone explain ...
1
vote
2
answers
180
views
C++-23 generic constraints of specific types
I would like to build a generic constraints of 128-bit data types in GNU C++-23 for template class and/or functions. Specifically, __int128 and unsigned __int128.
How do I achieve that? I know a bit ...
3
votes
1
answer
161
views
How to write a concept that checks for functions with variadic arguments?
I originally wanted to do this to create a concept that checks for allocators. I wanted to ensure that whatever template argument is passed, the following statement is correct:
std::allocator_traits::...
3
votes
0
answers
120
views
Accessibility of constraints used to constrain inherited constructors
I've come across an issue when trying to inherit from a class with constructors that are constrained via a requires clause. The constraint is defined via a private static constexpr member which ...
3
votes
1
answer
162
views
Constructor requires-constraint depending on another constructor existence
I would like to make a class template inheriting class from template parameter, with a set of constructors depending on inherited constructors.
This simple case with optional constructor from int ...
2
votes
1
answer
100
views
What is expected concept evaluation result for an incomplete type?
If I have an incomplete type, and evaluate some concept for it, for example some concept from the standard library:
#include <concepts>
int main() {
struct S;
return std::destructible&...
4
votes
1
answer
162
views
Copy constructor with impossible requires-constraint
In the following program, the copy constructor of struct S is declared with the constraint that the class is not copy constructible:
#include <concepts>
template <typename T>
struct S {
...
5
votes
2
answers
212
views
`requires` expressions and function namespaces
How can I define a C++ constraint that involves a namespace-specified function? Example:
template<typename T>
concept C = requires(T t) {
{ N::foo(t) } -> std::same_as<void>;
};
...
13
votes
2
answers
640
views
How can I disable concept evaluation result caching?
It appears that all compilers perform caching of concepts' evaluations. So if some concept was evaluated as false once, it will not be reevaluated again for the same argument:
template <typename T&...
3
votes
3
answers
118
views
need a concept to check if template argument is a member of a struct
In this code:
#include <iostream>
struct Messages
{
struct A{};
struct B{};
};
template<typename T>
concept IsMessages = requires {
typename Messages::A; // Need check that T ...
4
votes
0
answers
66
views
Is using `enum : bool` in requires clause allowed? GCC vs Clang [duplicate]
template <typename T>
struct S {
enum : bool { b = sizeof(S) > 32 };
void f() requires(b) { }
};
Clang happily accepts this code, while GCC fails with:
error: constraint expression ...
11
votes
1
answer
204
views
What constructors must be defined to satisfy copy constructible concept for any-like class?
I would like to make a class that satisfies std::copy_constructible concept and in addition can be constructed from an arbitrary copy constructible value much like std::any does:
#include <concepts&...
13
votes
0
answers
101
views
Are Mixins in C++ simply Policy-Oriented Design, or is there more to them?
Explanations of Mixins in a C++ context, but many of them seem to describe use cases that are essentially Policy-Oriented Design. This has led to some confusion on my part.
From what I understand, ...
3
votes
5
answers
172
views
Should I repeat concept constraints when using another constrained template?
I want to define a class template Bar<T>, which uses another class template Foo<U>, and the template parameter T is passed as the template argument of Foo<U> somewhere.
Then if U in ...
4
votes
3
answers
238
views
Providing constraints for a parameter pack
I have a templated class that contains a tuple.
template <class... SomeTypes>
class Foo
{ ... };
How can I apply a constraint on the types contained in the SomeTypes parameter pack?
For ...
3
votes
1
answer
163
views
Concepts and "deducing this"
I'm exploring the "deducing this" feature and I'm having trouble understanding how to use it correctly with concepts. Here's what I have:
#include <concepts>
struct X {
X() = ...