Questions tagged [c++20]
C++20 is the version of C++ after C++17. This tag should be used (along with the [C++] tag) for questions about C++ features specific to C++20.
4,942
questions
0
votes
0
answers
16
views
Conditionally using type defined in template parameter [duplicate]
I want to define a type the following way: if T has SomeType, then use T::SomeType, otherways use some other type. But if I try the following, the compiler compiles if T has no type called SomeType:
...
0
votes
0
answers
36
views
Member function call fails through shared_ptr inside lambda with access violation arising from std::basic_string
I'm having an extremely weird access violation problem with calling objects member function through shared pointer within std::function lambda. I have the following minimal code that I could use to ...
4
votes
1
answer
59
views
Difference between GCC and CLANG about using-directive scope in C++ modules
When I put an unexported "using namespace" directive in a C++20 module interface file, it also applies to the separate module implementation file under CLANG, but not under GCC. So, which ...
-1
votes
0
answers
26
views
Nesting concepts in requires clauses by constraining auto [duplicate]
I'd like to be able to make a concept to check some type (B) will be callable with another type (A), so long as A matches some concept. Instances of type B will be stored in some third type so it ...
-1
votes
1
answer
85
views
Getting index element of statically built std::array at compile time (attempt to access run-time storage error)
I'm trying to get the index of element by it's name at compile time however function result doesn't get evaluated at compile time.
I build a test project where I build a static std::array with ...
0
votes
1
answer
46
views
Boost MP11: Get type list from container of type indices
I know that with a combination of mp_with_index and mp_at_c, we can convert a single index into a type T:
using List = mp_list<Foo, Bar, Baz>;
std::size_t typeIndex = 2;
boost::mp11::...
1
vote
0
answers
24
views
C++ - CCLS Showing False Error on Concept Keyword
I'm trying to restrict types on a certain struct to only classes or structs. I did some digging and found this answer How To check if a template parameter is a struct. It works, and I've compiled it, ...
5
votes
5
answers
271
views
How to efficiently apply polynomials in C++ without a loop?
I want to get accurate approximations of some complex functions (pow, exp, log, log2...) faster than ones provided by cmath in C++'s standard library.
To do this I want to exploit the way the floating ...
8
votes
1
answer
113
views
Use of template keyword before dependent template name
Considering the following code example, I would expect to have to use the template keyword here to guide the compiler to treat variable v as a template. However, MSVC rejects the use of the template ...
4
votes
0
answers
63
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 ...
2
votes
0
answers
132
views
Why do std::ranges::binary_search and std::binary_search produce different results for the same input?
I don't understand why std::ranges::binary_search works different then std::binary_search. For example this kind of code:
std::vector<int> A = {0, 10, 12, 30};
cout << std::ranges::...
2
votes
1
answer
73
views
class template argument deduction for template alias of an alias (C++20)
I am using boost::outcome_v2::result with a custom type for the error and an alias to this type.
Now I am struggling to get class template argument deduction to work for my alias.
The code:
class ...
1
vote
1
answer
40
views
std::views:filter for some reason can't deduce argument type in a function pointer [duplicate]
I was learning how to use std::ranges and decided to experiment with c++23 std::views::enumerate and wrote something like this:
using namespace std;
auto isnt_nullptr(auto t) {
return get<1>(...
10
votes
0
answers
251
views
thread_local + std::thread deadlock on destruction
Anyone knows what kind of UB is this? The following code deadlocks on jthread destruction when built with MSVC 19.29.30148, sometimes it deadlocks after std::cout and sometimes before.
This is somehow ...
1
vote
1
answer
87
views
Can the read operations in `compare_exchange_strong` in different two thread read the same value?
[atomics.types.operations] p23 says:
bool compare_exchange_weak(T& expected, T desired, memory_order success, memory_order failure) noexcept;
Effects: Retrieves the value in expected.
It then ...
-1
votes
0
answers
75
views
C++ 20 Concept to determine if a class has a member function that returns a value that satisfies a ranges::view concept with specific value type? [duplicate]
I am trying to create a class concept that ensures member functions return a view to the user with a specific type.
For example if wanted to create class for a table of data and if the user only wants ...
3
votes
1
answer
67
views
Clarifying std::weak_ordering with C++20 Spaceship Operator for a Struct
I am learning about std::strong_ordering and std::weak_ordering in C++20. According to the docs, std::strong_ordering requires that equivalent values be indistinguishable, as opposed to std::...
1
vote
1
answer
50
views
C++20: A question about dynamic exception specification [duplicate]
Reference: Dynamic exception specification (until C++17)
I am writing some test code and would like to specify a throw exception of type std::bad_alloc (std::bad_alloc) for a template function, which ...
3
votes
2
answers
103
views
Program crashes when using modules partitions and implementation units
I have following program:
#include <cstdio>
import animal;
int main() {
auto s = makeSound();
printf("%s", s.c_str());
return 0;
}
and module interface:
module;
#include <...
1
vote
3
answers
97
views
How to overload spaceship operator (<=>) with reversed order?
I'd like to implement operator<=> to a custom type Bar with a reversed ordering.
struct Bar
{
auto operator<=>(const Bar& rhs) const
{
// How to implement <=> on `...
0
votes
0
answers
51
views
Boost Mp11: Check that every value in a map is a member of a given list
I have the following list and map:
using List
= boost::mp11::mp_list<EntityType, Name, Input, Position, Velocity,
Rotation, AnimationState, Interaction>;
using ...
0
votes
0
answers
42
views
Using a protected inner class as a base class of other class
In the section "11.9 Member access control" of the C++20 Standard there is provided an example (p.#7, example #2) of using a protected inner class of a class as a base class for a derived ...
-1
votes
0
answers
75
views
Best practices regarding C++ modules reachability [closed]
What are best practices regarding to idea of reachability in C++ modules?
I imagine having following scenario:
module;
#include <string>
export module animal;
export std::string makeSound() {
...
-1
votes
1
answer
23
views
How can i make a clickable button in console using C++?
I'm just trying to make a project in console. I want to add some buttons. Is there a way to add a clickable button in console?
I tried to find some information, but I couldn't find anything. Some ...
-3
votes
0
answers
57
views
trouble running a single cpp file through gcc in clion
I am studying the new features of C++20, using GCC as the compiler and Clion as the editor. I want to run each small feature as a separate cpp file.
When running the "hello world" program, ...
0
votes
0
answers
92
views
Is there a way to decompose a floating point value at compile time? [duplicate]
Essentially what I need to do is to find the exact rational equivalent of a floating point value at compile-time. In other words, given FLOAT_TYPE x (where FLOAT_TYPE is any one of float, double, or ...
1
vote
1
answer
84
views
C++ Spaceship operator <=> and std::sort with user defined type
I'm trying to use std::sort() on a vector of shared_ptr<Card>. I created a function that implements the spaceship operator. I couldn't simply overload it on Card because the container refers to ...
0
votes
0
answers
80
views
Why is the conversion from BGR to LCh much slower than the reverse in this C++ program?
I wanted an efficient way to convert an image represented by a three dimensional array of float values between 0 and 1 to an LCh D65 array, as a programming challenge.
I know I can use OpenCV's ...
2
votes
2
answers
93
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 ...
-3
votes
1
answer
56
views
clang not recognizing pointers from references from objects?
I'm trying to write a function that would work on a container of object, references, and pointer as well.
Can't figure out how to do it, given that clang does not returns true when using std::...
2
votes
0
answers
52
views
What's the difference between returning with a coroutine and resuming a coroutine from await_suspend?
The standard offers multiple ways to implement await_suspend in an awaitable type.
One way is returning with a coroutine handle to be resumed:
std::coroutine_handle<> await_resume(std::...
1
vote
0
answers
77
views
Calling the correct function specialization using a vector of variants
I have a vector of variants and several fully specialized functions. Now I want to call the correct function depending on the elements of my vector without having to check each individual element (...
0
votes
1
answer
45
views
GCC interrupts compiling with this message "internal compiler error: in instantiate_type" on simple coroutine
I get GCC compiler internal error message when compile my code. But MSVC and Clang compile it fine.
This is reduced to example one simple "yield coroutine".
I use x86-64 gcc 13.2 compiler ...
0
votes
1
answer
72
views
Core Constant expression in the hypothetical context as stated in the definition
Reference: constexpr specifier (since C++11)
constexpr variable
A constexpr variable must satisfy the following requirements:
...
it must have constant destruction, i.e. either:
it is of class type ...
0
votes
1
answer
80
views
How to pass a list of vectors to a lambda?
I'm trying to pass a list of vectors to a lambda expression but I'm receiving compilation errors. Here's my code:
#include <iostream>
#include <vector>
struct data_t {
int data;
...
1
vote
1
answer
58
views
Is there a std::view (or something like that) which occupies every nth element?
This is only a toy example, but assume I have a matrix like class
union {
std::array<T, M * N> m_x;
std::array<vector<T, M>, N> m_col;
};
and I want to access the ith row of ...
-1
votes
0
answers
44
views
Code::Blocks doesn't compile program but Visual Studio 2022 does [closed]
I am a beginner in C++, and I have the following program which compiles successfully in Visual Studio 2022, but fails to compile in Code::Blocks.
#include <algorithm>
#include <array>
#...
2
votes
2
answers
67
views
are there any predicate adaptors/buiders in std::ranges?
I am experimenting composition with ranges & views, but I cannot find any way to express common patterns of predicates.
I have this container of key-value pairs
std::vector<std::pair<int,...
0
votes
1
answer
86
views
Conversion of std::vector to std::span<T> [duplicate]
I'd like to generalize my code to take std::span rather than std::vector as parameter, but without losing the convenience of the auto-conversion you get when passing in a std::vector (see How can std::...
0
votes
1
answer
57
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>
...
7
votes
2
answers
271
views
All programs were UB before C++20?
Yesterday I heard this in a talk from David Stone
Prior to C++20 it was not possible to implement std::vector, all vector implementations, if they are written in C++, had undefined behavior.
But ...
1
vote
1
answer
44
views
CPU usage making multiple SSL client connections Boost.ASIO
I made an SMTPS client class based on Boost.Asio. I'm just wondering what I should expect in terms of CPU usage when sending an email using them. Right now as a stress test I queue up 15 on an ...
1
vote
1
answer
94
views
When should I overload the co_await operator?
I have recently discovered that the following code is legal:
struct S {
void operator co_await();
};
Why is co_await an overloadable operator? Why would I want to overload it, and what is the ...
1
vote
1
answer
64
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(...
1
vote
0
answers
46
views
Why non-type template parameters must have only public members in c++20 [duplicate]
See code below
#include <algorithm>
template<std::size_t N>
struct DoubleString
{
constexpr DoubleString(char const(&pp)[N])
{
std::copy(std::cbegin(pp), std::cend(pp),...
2
votes
2
answers
66
views
C++20 return a tuple in ternary statement [duplicate]
I have a small function that returns three values. I use a std::tuple to facilitate returning of multiple values.
What the function returns is determined by one variable, if the variable is not zero ...
2
votes
1
answer
43
views
Get parameter pack from boost mp_list
I'm struggling to filter a parameter pack while keeping it as a parameter pack.
Using boost::mp11, I have created a filtered mp_list of the parameter pack, but I don't know how to then use this list ...
3
votes
1
answer
48
views
Is executing boost::asio::~strand<> on that same strand supported?
Short version
Is executing boost::asio::~strand<> on that same strand supported?
Found documentation
The docs seems inconsistent, the "legacy/deprecated" boost::asio::io_context::...
1
vote
0
answers
47
views
Consuming C++ 20 modules library exports from a CMake project
I have a very simple CMake project using experimental C++ 20 modules to create a library. The library exports a class.
cmake_minimum_required(VERSION 3.26)
set(CMAKE_EXPORT_COMPILE_COMMANDS YES)
...
3
votes
3
answers
113
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 ...