Questions tagged [c++-concepts]

Concepts (formerly known as "Concepts Lite") in C++ is a language feature designed to improve support for generic programming. Questions referring to this feature or to the boost "concept check" library should use this tag; you may also wish to use one or more of the tags [c++], [boost], [template-meta-programming] and [metaprogramming] depending on your question.

Filter by
Sorted by
Tagged with
4 votes
2 answers
67 views

Concept to define type A to be equal to type B, if B exists [duplicate]

I want TagOrInt<T> to be equal to T::Tag if T has a member type Tag, and equal to int otherwise. Like: template <class T> using TagOrint = typename T::Tag; // if this is valid template <...
Sven Sandberg's user avatar
-3 votes
0 answers
34 views

C++ - Concept as class method parameter - unable to build [duplicate]

i just started learning C++ at a deeper level and i came across this problem: I am trying to build a simple program with three files: main.cpp cylinder.cpp cylinder.h When i try to build this code, ...
Alberto Venturelli's user avatar
1 vote
1 answer
79 views

Why aren't concept constraints considered contextual type information?

Consider: void f(int); // (1) void f(int, int); // (2) auto pf1 = static_cast< void (*)(int) >(f); // Ok, address of (1) auto pf2 = static_cast< void (*)(int, int) >(f); // ...
Hank's user avatar
  • 322
0 votes
1 answer
63 views

Constrain an argument to an inner type of a template class

I'd like to have a concept, which restricts the type to a nested class of a template (e.g. a std::vector<...>::iterator). I can't change the template and its nested class. template<class T>...
krzikalla's user avatar
1 vote
0 answers
44 views

C++ concepts and boost geometry

I have an operator template, that calls a boost geometry function template for types T1, T2. If this function template cannot be instantiated for types T1, T2, then I want the compiler to choose a ...
Acave At's user avatar
1 vote
2 answers
74 views

How to manually disambiguate partial specializations when using C++20 concepts?

If I define two concepts and have partial specializations of a class for each one, then clearly if I have a type that satisfies both, I have an ambiguity: #include <cstdio> template <...
gct's user avatar
  • 14.2k
0 votes
0 answers
48 views

C++20 Concept for Templated Method - non const lvalue reference cannot bind to rvalue [duplicate]

I am trying to write a concept to require the given type to have a templated method, but am struggling to make the concept recognize that I have implemented the method correctly. I have an interface ...
larkwiot's user avatar
0 votes
1 answer
145 views

How to cast an arbitrary std::integral type to unsigned?

I am trying to cast an std::integral type to unsigned when a special flag is set i.e. it should simply be interpreted as an unsigned number in this case. This cast is necessary in that case, because ...
StckXchnge-nub12's user avatar
19 votes
6 answers
2k views

How can I constrain template parameter pack arguments to a "chain" sequence?

Suppose I have two classes: template <typename X, typename Y> class Functor {}; template <typename Start, typename End, typename ...Functors> class Template {}; Template has the ...
SKNB's user avatar
  • 383
0 votes
1 answer
96 views

How to check if a template typename is the same as another type but ignoring template parameters

I want to make a library that provides units of measurements, such as length, mass, etc as types. I know such libraries probably exist already, but I want to learn how to do such a thing myself. I ...
FalcoGer's user avatar
  • 2,366
1 vote
2 answers
97 views

C++ Concept to determine arity and type of functions

The following code intends to provide a concept for functions with a given signature and arity: #include<utility> //std::index_sequence #include<cstddef> //std::size_t #include<...
davidhigh's user avatar
  • 14.8k
1 vote
1 answer
126 views

C++ Concepts for templated types

Given one has similar types: template<typename T> using a_T = std::unique_ptr<T[]>; template<typename T> using b_T = std::unique_ptr<a_T<T>>[]>; template<typename T&...
schnedan's user avatar
  • 244
2 votes
1 answer
62 views

Resolve Template Parameter Pack Overload Ambiguity

Minimum Example I have the following struct with a few specializations: template<size_t ...Tpar> struct Base{}; struct X{}; struct Y{}; template<typename T, size_t ...Tpar> struct Spline:...
kaisong's user avatar
  • 149
1 vote
0 answers
76 views

template method in a C++ 20 'concepts'

I would like to define a concept which requires a template method, like template <typename t> concept event = std::default_initializable<t> && std::copy_constructible<t>; ...
canellas's user avatar
  • 563
1 vote
1 answer
99 views

template variadic function constraint

As following codes I got "the associated constraints are not satisfied" compiler (MVC19) error. But without variadic parameters it works fine. Thank you in advance for your support. ...
VeNToR's user avatar
  • 77
0 votes
1 answer
78 views

using c++20 concepts to find nth element of parameter pack [duplicate]

I am looking for a non-recursive, modern method (which compiles, at least with gcc and clang) for finding the 𝑛th element of a variadic parameter pack. Indeed, it would seem such a solution, using c++...
phoko's user avatar
  • 1
4 votes
1 answer
120 views

How to summarize the meaning of the C++20 iterator-concepts?

If I want to visualize the hierarchy of the C++20 iterator concepts I want to briefly explain in one line what each concept is about. Nothing precise, just a rough meaning (the precision is in the ...
towi's user avatar
  • 21.8k
-2 votes
1 answer
68 views

how to create static member function from class template typedef argument?

I'm trying to build a class template where one static methodneeds to have the typedef specified in the template argument. The goal is to specify a typedef like this typedef foobar = void __stdcall foo(...
Jon's user avatar
  • 1
4 votes
1 answer
94 views

How to check if a type alias exists in a type using concepts

Is there a way to require that a given type has a type alias defined within it? For example, how could I write a concept to check that this would be valid where the concept checks that the type T has ...
Keltek's user avatar
  • 522
6 votes
2 answers
124 views

How to define a 'concept' that uses a 'concept' in C++ 20?

I am studying concepts in C++ 20, and came up with the need of defining a concept using a previous defined concept. So, in the example below I expected f<pub_b>() to generate a compiler error, ...
canellas's user avatar
  • 563
0 votes
0 answers
52 views

How to constrain an overload on an `input_range` parameter to be on int-ranges only? [duplicate]

If I overload << for input_range like this: std::ostream& operator<<(std::ostream&os, input_range auto&& data) { for(auto &e : data) { os << e << ' ';...
towi's user avatar
  • 21.8k
0 votes
1 answer
77 views

What can the implementation do with semantic requirements for concepts?

I'm confused about what the purpose and meaning of semantic requirements for concepts is. For example, [iterator.concept.contiguous] p2 lists a number of semantic requirements for the std::...
Jan Schultke's user avatar
  • 22.4k
8 votes
1 answer
364 views

How to reuse the return type of a function call inside a requires expression?

I'm writing a concept that checks if a type can be used in an expression that composes 2 functions: template<typename T> concept C = requires(T t) { f(g(t)); }; i.e., I want to check if for ...
cigien's user avatar
  • 58.7k
4 votes
0 answers
95 views

Class template specialization using concepts

I'm attempting to use concepts to chose between different specializations of class templates. Unfortunately, 3 different compilers give 3 different results: gcc: compiles ok clang: Accepts the ...
Rob190's user avatar
  • 65
3 votes
0 answers
92 views

Need of template specifier to treat a type as a dependent template name in concept requires clause

Disclaimer: This question is out of pure curiosity, I do not intend to use such a construct (I can't even think of a relevant use-case for such a concept). If we consider the following concept (to ...
Fareanor's user avatar
  • 6,333
2 votes
3 answers
236 views

How to define a concept which checks whether a class can have different type of template parameters

Using C++ type system and type arithmetic, I want to express the general concept of a mathematical ring. This is, to simplify, a set with zero, one, addition, multiplicatio and substraction. To keep ...
Regis Portalez's user avatar
6 votes
1 answer
120 views

Ambiguous template specialization with concepts and real types: which compiler is right?

Consider the following code: #include<concepts> template<typename> void foo() { } template<std::integral> void foo() { } template<> void foo<bool>() { } int main() {...
Jean-Michaël Celerier's user avatar
5 votes
1 answer
167 views

C++20 Concept/requires with std::map emplace not working as expected

I'm experimenting a bit with c++20 concepts and requires, and tried to make a concept that checks valid parameters for a std::map emplace call. Very similar for example as at C++20 concepts how to ...
DanRechtsaf's user avatar
0 votes
0 answers
99 views

C++ Storing different range operations in the same variable

I'm implementing a custom iterator that is parametrized with range types: template <std::ranges::range Container> class CustomIterator{} The iterator is then used like this: auto t = ...
yonutix's user avatar
  • 2,124
0 votes
1 answer
63 views

C++ pass function as template argument within constructor

I'd like to replace a std::function with a template parameter, since everything is known at compile time. No I know there a many answers here in the forum and over the internet how to pass and ...
JHeni's user avatar
  • 491
4 votes
1 answer
134 views

Why is an std::vector of noncopyable elements copyable (according to concepts)?

#include<concepts> #include<vector> struct nc { nc(const nc&) = delete; }; static_assert(!std::copyable<nc>); // OK static_assert(!std::copyable<std::vector<nc>>); ...
Nikola Benes's user avatar
  • 2,489
1 vote
1 answer
95 views

Writing a c++ concept for a type with static member function whose arguments start with a specific type

I have the following factory pattern in my code: class Base { // New constructs the concrete type, but returns a ptr to base // ... here represents actual parameters continuation (like int, ...
Elwardi's user avatar
  • 91
5 votes
1 answer
111 views

How can I check if my concept is sufficient for my function's implementation?

When I write a template function, I'd like to use concepts, and also to be confident that I haven't used something in my function over and above the "contract" that the concept specifies. ...
TooTone's user avatar
  • 7,463
3 votes
1 answer
98 views

How are the immediately-declared constraint-expressions of type-constraints combined?

If C is a concept, are the following constructs equivalent? template <C A, C B> template <typename A, typename B> requires C<A> && C<B> In other words, are all the ...
Jan Schultke's user avatar
  • 22.4k
3 votes
1 answer
110 views

c++20 partial class specialization with concepts and variadic template args

Looking to understand why the code below results in: class template partial specialization is not more specialized than the primary template template <typename T, std::integral... Us> struct ...
serine's user avatar
  • 1,370
6 votes
2 answers
184 views

How can you enable a class template member function only if a template argument was provided?

Is it possible to have a class with an optional template parameter that can be called like this?: #include <iostream> template <typename T = void> class A final { public: // This ...
yonutix's user avatar
  • 2,124
12 votes
1 answer
1k views

Why does C++ have no std::invocable_r concept?

C++20 added concepts, and the standard library includes quite a few of them. One concept in particular caught my eye: std::invocable which validates that a functor can be invoked with a set of ...
Afiefh's user avatar
  • 1,047
3 votes
4 answers
118 views

How to use decltype(T::member) for a member that might not exist in T

I am looking for a way to define a variable with a type depending on the type of the member of a type my class is templated on, with the additional caveat that the member variable might not exist. As ...
DottyPhone's user avatar
4 votes
0 answers
76 views

Template template parameter with a concept - why does GCC reject it?

I'm writing a generic struct that is templated on another template. I want the inner template to only accept types that match a concept. template<template<std::regular> typename T> struct ...
samw's user avatar
  • 139
2 votes
2 answers
115 views

C++20 use concepts to enforce interfaces on classes

I'm creating a wrapper for a json decoder. Depending on the actual target different json parser 3rd parties are in use. All of them share a common interface. class IJsonDecoder { public: /** * @brief ...
JHeni's user avatar
  • 491
0 votes
1 answer
44 views

How to provide information for library code client when there are only concepts?

Lets consider following situation, I'm writing library and instead of runtime polymorphism which comes with cost, but have clear and visible interface with all declarations, I choose generic ...
Kazz's user avatar
  • 1,062
0 votes
1 answer
71 views

Why does `std::span` require an explicit constructor to use in a templated function? [duplicate]

Godbolt link. I have the following function: #include <algorithm> #include <iostream> #include <numeric> #include <ranges> #include <span> #include <type_traits> ...
SRSR333's user avatar
  • 182
1 vote
1 answer
76 views

Parameter pack which only contains types convertible to T

Consider the following simple vector class: #include <array> #include <concepts> template<typename T, std::size_t D> class vector { public: vector() = default; vector(...
0xbadf00d's user avatar
  • 17.7k
0 votes
0 answers
27 views

Variadic Template Argument Function with Concepts Failed to be Invoked

I needed a function that can accept variable no. of arguments. Each one, a pair of the same type. I used this to define the concept. With the following definition of variadic template argument ...
Askr Askr's user avatar
3 votes
3 answers
122 views

How to define a C++ concept that checks if a given template type parameter is a sequential container of pair<T, T>

How can I define C++ concept that checks if a given template type parameter is a sequential container or a range, of pairs (e.g. vector<pair<string, string>>. Let's say the name of the ...
Askr Askr's user avatar
-1 votes
1 answer
62 views

Is any of concept when using wrapper struct to hold list of types

Some of the code I'm writing is parameterized by a list of types, and, along the lines of this answer, I'm using a wrapper struct template <typename... T> struct types { }; so that I can write ...
TooTone's user avatar
  • 7,463
0 votes
0 answers
52 views

Using concept inside class

I have a trait class that specifies base classes and derived classes for a class hierarchy. Some of the classes are templates, and I would like to have some concepts to help working with them. ...
Magnar Myrtveit's user avatar
0 votes
2 answers
63 views

Constexpr matching wide vs narrow strings types

I have a wrapper around fmt that prints to visual studio debug output. I tried to add wide string support: template <typename Arg, typename... Args> void fmt(Arg&& arg, Args&&... ...
Tom Huntington's user avatar
0 votes
1 answer
56 views

How to detect if variable template specialization exists using concept?

I want to check is if I can safely use MyTrait_v<T> (not MyTrait<T>::value) using the concept HasMyTrait_v<T>. Here is my code, of course, it doesn't work. #include<concepts> #...
J.Doe's user avatar
  • 1,845
3 votes
3 answers
78 views

Variadic Templates with Concepts

I've tested variadic templates inside a function, which I developed step by step. template <typename... Ts> void foo (Ts...) noexcept {} // Test code int i {123}; int j {456}; foo(1,2,3,i,j); ...
Code4Fun's user avatar
  • 127

1
2 3 4 5
23