Skip to main content
Filter by
Sorted by
Tagged with
Advice
1 vote
1 replies
90 views

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 ...
ValeriyKarasikov's user avatar
2 votes
1 answer
182 views

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> ...
Jens's user avatar
  • 143
3 votes
0 answers
118 views

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<...
Thibaut's user avatar
  • 2,450
3 votes
2 answers
185 views

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 ...
Jepessen's user avatar
  • 12.7k
2 votes
1 answer
140 views

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); }...
Oersted's user avatar
  • 3,834
7 votes
2 answers
222 views

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, ...
Zebrafish's user avatar
  • 16.3k
6 votes
0 answers
234 views

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 ...
Jack's user avatar
  • 61
2 votes
2 answers
111 views

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 ...
Raildex's user avatar
  • 5,428
22 votes
3 answers
2k views

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 { ...
HolyBlackCat's user avatar
4 votes
2 answers
182 views

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,...
RocketSearcher's user avatar
15 votes
1 answer
403 views

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 ...
Sven Sandberg's user avatar
2 votes
0 answers
95 views

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(); ...
master_clown's user avatar
5 votes
1 answer
168 views

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 : ...
Fei Du's user avatar
  • 80
4 votes
1 answer
181 views

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<...
Mugnus Daisy's user avatar
4 votes
2 answers
193 views

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 ...
Zebrafish's user avatar
  • 16.3k
16 votes
1 answer
707 views

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 ...
Remy Lebeau's user avatar
2 votes
2 answers
135 views

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() (...
Sergey Kolesnik's user avatar
15 votes
1 answer
335 views

template<typename T, typename... Args> inline constexpr auto c = sizeof(T) + sizeof...(Args) > 1; template<typename... Args> requires c<Args...> // ok void f1() { } template<...
xmllmx's user avatar
  • 44.6k
5 votes
0 answers
213 views

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::...
blst's user avatar
  • 157
4 votes
2 answers
162 views

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 { ...
Wei Li's user avatar
  • 1,947
7 votes
1 answer
148 views

Clang currently rejects the following code: template <typename X> constexpr bool is_valid = true; template<typename T> class Nesting { public: template<typename Q> requires ...
Bernd's user avatar
  • 2,469
2 votes
2 answers
110 views

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 ...
nicolai's user avatar
  • 1,244
19 votes
2 answers
1k views

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 ...
Sven Sandberg's user avatar
0 votes
0 answers
107 views

#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) { ...
xmllmx's user avatar
  • 44.6k
3 votes
0 answers
126 views

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 <...
MarkB's user avatar
  • 2,230
11 votes
1 answer
565 views

Here's the test code: #include <concepts> template<typename T> concept Printable = requires(T obj) { { obj.print() } -> std::same_as<void>; }; template<Printable T> ...
Annyo's user avatar
  • 1,607
4 votes
1 answer
286 views

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::...
Ichthyo's user avatar
  • 8,471
1 vote
0 answers
73 views

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 ...
rdong8's user avatar
  • 97
3 votes
1 answer
152 views

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> && ...
xmllmx's user avatar
  • 44.6k
2 votes
1 answer
189 views

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 ...
user30708321's user avatar
4 votes
1 answer
226 views

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 ...
undefined's user avatar
  • 228
5 votes
2 answers
184 views

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 ...
bers's user avatar
  • 6,309
7 votes
1 answer
284 views

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: ...
rubenvb's user avatar
  • 77.2k
0 votes
2 answers
185 views

From cppreference: template< class I > concept contiguous_iterator = std::random_access_iterator<I> && std::derived_from</*ITER_CONCEPT*/<I>, std::...
rdong8's user avatar
  • 97
-1 votes
2 answers
101 views

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 ...
Zebrafish's user avatar
  • 16.3k
1 vote
2 answers
180 views

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 ...
khteh's user avatar
  • 4,280
3 votes
1 answer
161 views

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::...
Osama Ahmad's user avatar
  • 2,368
3 votes
0 answers
120 views

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 ...
Die4Toast's user avatar
  • 123
3 votes
1 answer
162 views

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 ...
Fedor's user avatar
  • 24.7k
2 votes
1 answer
100 views

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&...
Fedor's user avatar
  • 24.7k
4 votes
1 answer
162 views

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 { ...
Fedor's user avatar
  • 24.7k
5 votes
2 answers
212 views

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>; }; ...
Paolo M's user avatar
  • 12.9k
13 votes
2 answers
640 views

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&...
Fedor's user avatar
  • 24.7k
3 votes
3 answers
118 views

In this code: #include <iostream> struct Messages { struct A{}; struct B{}; }; template<typename T> concept IsMessages = requires { typename Messages::A; // Need check that T ...
edwinc's user avatar
  • 1,722
4 votes
0 answers
66 views

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 ...
Vittorio Romeo's user avatar
11 votes
1 answer
204 views

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&...
Fedor's user avatar
  • 24.7k
13 votes
0 answers
101 views

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, ...
Bernard Gressing's user avatar
3 votes
5 answers
172 views

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 ...
Timothy Liu's user avatar
4 votes
3 answers
238 views

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 ...
Rob W.'s user avatar
  • 404
3 votes
1 answer
163 views

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() = ...
Derek's user avatar
  • 211

1
2 3 4 5
28