Questions tagged [c++17]
C++17 is the name of the C++ standard approved in 2017. It builds upon the previous C++14 standard, improving the core language and standard library, and adding a few new language features.
9,274
questions
-2
votes
1
answer
37
views
Is this causing a dangling pointer when using map of pointers
This simple code generates a warning about "Object baking the pointer will be destroyed at the end of the full expression". What does that mean? Can I not use the object entry after I use ...
-2
votes
0
answers
40
views
Why the pathfile errors when I include <bits/stdc++.h> [closed]
in the c++17, when I include <bits/stdc++.h>,the filepath errors, it says two pathfile cannot be compared by != .It works in c++14.
0
votes
1
answer
36
views
Proper way to initialize unique_ptr as class members wrapping C 'structs'
I'm trying to implement some openssl functionality in a C++ program. The openssl library requires a lot of memory allocation / deallocation. My approach is to pack the required structs from openssl ...
0
votes
0
answers
71
views
What can I do for better object orienting? [closed]
I got a lot of questions about my original code (see my profile for a really confusing code). My reviews from the teacher was that the use of pointers was horrendous and it wasn't OOP enough. This ...
-2
votes
0
answers
59
views
C++ Read from the back of a .txt file
How would one go about reading an entire string from the end of a .txt file to the start line by line.
I have seen similar posts but only involving chars and not a string.
The code I have:
void ...
0
votes
2
answers
115
views
How to implement a lambda within a depth first function
I'm programming a family tree, and I was instructed to use lambda in the depth first search. I've tried to implement it, and I understand the basics of lambdas. I can't for the life of me understand ...
2
votes
2
answers
72
views
How can I solve this weird error while using map in c++
I'm getting stuck in a weird infinite loop while using map in c++.
The first code works good and outputs numbers.
The test cases are:
5 2
1 2 3 4 5
long long N, K, count = 0;
cin>>N>>K;
...
0
votes
0
answers
26
views
hard time understanding overriding virtual private methods [duplicate]
I have this code snippet. I come from a Java background. I don't understand why the BubbleSort class sort method can access a private method in its base class and override it? This code snippet ...
5
votes
1
answer
43
views
isatty what header to include
All of the examples I see in manpages and online suggests <unistd.h>.
CLion suggested the more c++ friendly <csignal>.
Looking into <csignal> on my g++ setup, I ended up in a rabbit ...
-2
votes
0
answers
69
views
Why do C++14 and C++ 17 give different answers? [closed]
Why does the code below give me two different results if I use C++ 14 and C++17? (C++ 17 gives correct answer)
#include <bits/stdc++.h>
void solve(){
long long n;
std::cin>>n;
...
0
votes
2
answers
53
views
Dynamic load a class from a dll in Windows
I want to implement a multiplatform plugin system on an application that I am working, but I am unable to make it work on Windows.
The proposal of this plugin system is to add the posibility of to ...
1
vote
1
answer
60
views
Does modern compilers use copy elision when using the builder pattern
I am using a few builder patterns in my code base and I was wondering whether return by value should be favoured over the return by reference, given that is the push I am feeling with modern C++. The ...
0
votes
1
answer
46
views
using declval with reference types
I would like to understand how the assignment of int & to double below in conjunction with declval works? Is T deduced to something other than int & ?
#include <iostream>
#include <...
1
vote
2
answers
81
views
generic decorators for callable objects with conditional return
I want to write decorator functions for callable objects.
This is what I have now:
#include <utility>
template <typename DecoratedT, typename CallableT>
constexpr auto before_callable(...
0
votes
1
answer
23
views
How to find the count of sub numbers of size k which divide the number
Given a number n
Find the count of the sub numbers of size x in a number num which divides num.
For example, if the number is 250
and x=2
the answer will be 2
as 250%25==0
and 250 % 50==0.
Can anyone ...
-2
votes
0
answers
19
views
Why does the method not retrieve the data from the binary file the second time I run the program? [closed]
The problem is that I save the objects of a class and the method works better, if I try to read them it also reads them very well, the problem comes when the program finishes executing, the next time ...
1
vote
1
answer
31
views
Expand variadic template template parameters for use in e.g. std::variant<T...>
This will be a hard nut to crack. I don't even know if it's possible.
My goal is to create a receive function that listens to multiple queues and pastes the object received via the particular queue (...
-2
votes
0
answers
35
views
Efficiently store a collection of n-bit precision numbers [closed]
I am currently storing the data coming from an ADC in a std::vector. This data has 12 bit precision (from 0 to 4095). Currently I store this data in a std::vector<short int> which uses 16 bits ...
0
votes
1
answer
52
views
How to create a simple version of std::function class
Suppose that I want to create a simple version ofstd::function, which has following behaviors:
1. function(){} -> A void constructor
2. function(_ReturnType, Args... vlist) -> To convert ...
0
votes
1
answer
19
views
invalid declarator before std::variant
I'm trying to implement an ad-hoc light weight state machine using std::variant. However, it seems that the variant fsm isn't declared right as it fails with the following errors:
<source>: In ...
2
votes
1
answer
51
views
Generating compile time functions string for formatting strings with libfmt
I want to create a nice table in stdout. The table has a lot of headers that are mainly compiletime strings. For example:
std::cout << fmt::format("|{0:-^80}|\n", "File ...
1
vote
0
answers
55
views
Why does std::map work, but std::unordered_map throw an error when used in a struct [duplicate]
The top struct declaration works, but the bottom fails, why?
class Trie {
public:
struct Node {
map<char, Node> map;
};
};
class Trie {
public:
struct Node {
...
2
votes
1
answer
51
views
Private compile-time -> run-time adapter. Strange compilation error
Basically, this code fails with a very strange error:
<source>:75:29: error: cannot convert 'get_view<main()::<unnamed struct>, run_time::data_view>::operator()(const main()::<...
0
votes
0
answers
25
views
Use OpenCV GpuMat object in CUDA functions [closed]
I am building a GPU-based application where CUDA Toolkit functions are used for inferencing. The pre-processing happens in the CPU, and then the input image is copied to the GPU memory and inferencing ...
-2
votes
1
answer
34
views
Find the missing digit in a equation
Hello I want to find the missing number in the equation for example. 7+x=17. x=10. This is what I want to do I think the problem in my code is that the code doesn't know how to identify which digit is ...
0
votes
0
answers
23
views
How can I set my include files in clang with c++?
[platform] MacOS M1 Max with llvm installed by homebrew
Homebrew clang version 13.0.1.
Target: arm64-apple-darwin21.4.0.
Thread model: posix.
InstalledDir: /opt/homebrew/opt/llvm/bin
xcode-select ...
1
vote
2
answers
85
views
Multi-threading slower than single thread
I am new to parallel programming. I have been playing around with multi-threading and for some reason, multi-threading the Mandelbrot set is slower than running a single thread. I have been trying to ...
0
votes
1
answer
69
views
how to iterate through several vectors with DIFFERENT types?
Here, I have a custom collection of entities (vectors of different types: Type1, Type2, Type3 , Type4, for simplicity I reduced to two types) with different sizes:
#include <stdio.h>
#include &...
0
votes
0
answers
61
views
How to define recursive std::variant?
I used to ask a question about recursive std::variant. According to comments, recursive std::variant is an undefined behavior, so I tried some tricks to make recursive std::variant legally.
The code ...
0
votes
0
answers
46
views
Parsing Function Calls with `constexpr` library in C++
I'm at the end of the semester and need to get this in, I have been struggling for the last few days to implement function calls into my language. I am using the ctpg library which implements a ...
-4
votes
0
answers
38
views
I don't know to give input for a string from the user [closed]
string s;
getline(cin,s);
Like this we are taking input from user. If i don't know the input, default string s will need to store "NULL". How can I do that
3
votes
1
answer
51
views
What will happen if I pass a mutable lambda to a function as const reference?
What does the code actually do when I pass a mutable lambda as const reference?
Why does not the compiler raise error, is this an undefined operation?
Why f1 and f2 are differnt which f1 uses std::...
2
votes
1
answer
73
views
Does std::optional copy an object containing a std::unique_ptr
I'm trying to return a std::optional to an object containing a std::unique_ptr member. However, when I try to change the optional return type to use a reference, I get errors like this:
error: cannot ...
1
vote
3
answers
71
views
Compiler macro to compare byte size of types
Can this if-statement be replaced with a #if .... macro?
Possibly without having to include (too many) extra headers.
#include <cstdint>
#include <string>
///Parse a string to obtain a ...
1
vote
0
answers
32
views
Determine maximal number of "parallel execution agents" in the parallel STL
Is there a way to know a maximal number of parallel execution agents (not sure what the term is) in the parallel STL algorithms?
The reason I need this information is to pre-allocate memory needed by ...
0
votes
0
answers
27
views
Aliasing a placeholding buffer in base class from derived class
I want to create an array of struct objects that each contain a member which holds a value. However, this value might differ in its type from object to object. As the size can't vary I decided to put ...
0
votes
1
answer
102
views
c++ constexpr concatenate char*
Context:
In my company we generate a lot of types based on IDL files. Some of the types require special logic so they are handcoded but follow the same pattern as the generated ones. We have a ...
18
votes
3
answers
1k
views
What is wrong with my application of SFINAE when trying to implement a type trait?
I needed a type trait that decays enums to their underlying type, and works the same as decay_t for all other types. I've written the following code, and apparently this is not how SFINAE works. But ...
0
votes
1
answer
101
views
Why no std::as_const overload for pointer types
I just came across std::as_const and I was surprised by the output of the last line in the following snippet:
#include <cstdio>
#include <utility>
struct S {
void foo() { std::puts(&...
0
votes
0
answers
34
views
Optimization-friendly Polymorphic NVI adapter class for Runtime Polymorphism (customizable ownership)
I have a polymorphic NVI class to provide an adapter for Runtime-Polymorphism.
Type-erasure is used for loose coupling.
The non-owning implementation can be optimized out in a compile-time context:
// ...
0
votes
1
answer
46
views
User-defined deduction guides vs factory functions
When writing generic wrapper class templates like the FooWrapper<WrappeeT> below, it's convenient to deduce WrappeeT when creating the instance of the wrapper class. For example, just type ...
2
votes
0
answers
67
views
Plain enum to enum class
is it possible to perform some kind of casting to convert plain enum to enum class? E.g.
enum PlainEnum {
None = 0,
Foo,
Bar
}
Some converstion I'm looking for:
enum class ScopedEnum = some_cast<...
0
votes
1
answer
59
views
Is there some way to define a variable as a function such that calling the variable at some given time will return the function's output at that time? [closed]
Essentially, I'm trying to do something like
#define foobar foo.bar()
But without the use of #define, so I can write something along the lines of
double foobar = foo.bar();
Obviously, compiling the ...
0
votes
1
answer
78
views
Modern C++ containers with default initialization
Is it still possible to change the initialization in C++ containers to default initialization instead of value initialization?
There is a method below that modifies the allocator to change it, but std:...
2
votes
1
answer
64
views
Does providing an explicit deduction guide disable the generation/formation of implicit deduction guides
I am reading about deduction guides in C++17. So say we have the following example:
template<typename T> struct Custom
{
};
template<typename T> struct Person
{
Person(Custom<T&...
1
vote
1
answer
64
views
Does constexpr really imply const? [duplicate]
Compare the following:
I have a static member in a class that is either const constexpr or just constexpr. According to this explanation on MS Docs constexpr implies constness:
All constexpr ...
-3
votes
1
answer
46
views
During compilation, check that data members used in a function are also used in another function of the same class
Need to fail the build if data members assigned in a function are not used in another function of the same class. For example, with the below definition, I would like to enforce that data members used ...
1
vote
1
answer
68
views
Parameter pack referenced but not expanded in a using declaration: compiler bugs or not?
Consider the following code (also available here on compiler explorer)
#include <utility>
#include <type_traits>
template <std::size_t N, class = std::make_index_sequence<N>>
...
-1
votes
0
answers
24
views
C++ Vscode Terminal Input Doesn't Work For Dynamic Input i.e. Vectors/For loops
I've just started coding in C++ with VSCode and I followed the setup here. It works perfectly with the coderunner extension in the terminal, however I am unable to use std::cin inside of a for loop. I ...
5
votes
2
answers
126
views
Why is returning a const from a function not being detected as a const?
I have a program which depends on the result of std::is_same_v <const value_t, decltype(value)>. However, I have found that when functions are passed to this expression the result is unexpected, ...