1,364 questions
2
votes
2
answers
153
views
How to avoid implicit conversion in C++ concepts?
Is there a way to prevent implicit conversions happening in concepts? For example, this example compiles with GCC and clang when I actually want it to give an error that the char constructor is ...
5
votes
1
answer
205
views
SFINAE vs concepts deliver different results for enum type
Only for academic reasons I try out SFINAE vs. concepts and wrote my own traits. I know there is std::is_integral and a lot of other stuff.
I use decltype( ptr+ T{}){}; to check if it is an integer ...
Advice
1
vote
1
replies
102
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 ...
7
votes
2
answers
406
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
131
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
199
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
156
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
224
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
246
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
112
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
185
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
406
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
97
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
170
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 : ...