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.

1
vote
0answers
42 views

std::nothrow for non-allocation contexts

In my perusal of questions related to std::nothrow, the context is always memory allocation. Fine, that appears to be what its intended purpose is. However, in the libraries my team and I provide, ...
1
vote
3answers
47 views

Reversing an array (vector) using reverse_iterator

I'm trying to improve my c++ knowledge using Edabit challenges and testing out 'new' functions. Current challenge wants us to write a function to reverse an array. examples: reverse([1, 2, 3, 4]) ➞ [...
0
votes
2answers
58 views

Safely convert std::string_view to int (like stoi or atoi)

Is there a safe standard way to convert std::string_view to int? Since C++11 std::string lets us use stoi to convert to int: std::string str = "12345"; int i1 = stoi(str); // Works, ...
9
votes
1answer
122 views

Why does std::string_view create a dangling view in a ternary expression?

Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling ...
0
votes
1answer
27 views

Displaying garbage value

When the friendly function add is used to print the value of sum variable of two classes, the correct output is printing. But, when then display function of their respective class are used, garbage ...
0
votes
0answers
28 views

Processing triggers for libc-bin (2.28-0ubuntu1) … /sbin/ldconfig.real: /usr/lib/libgraph.so.1 is not a symbolic link [on hold]

Can someone help me with this problem i have been facing this problem since i have installed libgraph-1.0.2 in my Ubuntu 18.10. Actually i was trying to use graphics.h library in my program so while ...
2
votes
2answers
127 views

Why is there no 'aligned_realloc' on most platforms?

MSVC has its own non-standard functions _aligned_malloc, _aligned_realloc and _aligned_free. C++17 and C11 have introduced (std::)aligned_alloc, results of which can be deallocated with free or ...
1
vote
1answer
41 views

the return of function “if constexpr” and “else if constexpr”

I'm the beginner with C++. Could some one explain the output after compile process function. Thanks a lot. template <typename T> auto process(T arg) { // 4. With constexpr if, to ...
0
votes
2answers
74 views

Passing std::filesystem::path to a function segfaults

When I attempt to use std::filesystem::path as a function argument, it segfaults on my machine. Here is a minimal example: #include <filesystem> void thing(const std::filesystem::path& p) {...
5
votes
2answers
164 views

Set file creation time with standard library only

C++17 includes a new and portable API for working with filesystems (<filesystem>). My program unpacks an archive. This archive contains files, directories and creatation time for each file. ...
1
vote
1answer
65 views

Is it possible to store a reference in a std::any?

I was trying some things and came to the following question: Is there a possibility to store references to a value in a std::any? I tried the following approaches: #include <any> #include <...
-1
votes
0answers
38 views

Library with optionalifier template in C++ [on hold]

Is there a C++ library that allows for the "optionalification" of C++ function pointers? Such that if you have a function std::vector<int> foo(int, std::vector<int>) then optionalify<...
0
votes
0answers
23 views

Procedure entry point K32EnumProcessModules could not be located in the dynamic link library KERNEL32.dll [duplicate]

My C++ application fails to launch on a Windows 2008 SP2 machine with the following error. My-Application.exe - Entry Point Not Found The procedure entry point K32EnumProcessModules ...
0
votes
1answer
37 views

How to avoid code duplicate const and non-const collection processing with lambda

The answer here does not work for this pattern in C++17: template <typename Processor> void Collection::ProcessCollection(Processor & processor) const { for( int idx = -1 ; ++idx < ...
9
votes
1answer
245 views

Should methods returning const std::string& return const std::string_view instead?

Assume we have a simple getter method in a class that returns a const reference to a std::string member: const std::string& getString() const noexcept { return someString; } With the advent of ...
19
votes
6answers
995 views

Explicit direct #include vs. Non-contractual transitive #include

Say we have this header file: MyClass.hpp #pragma once #include <vector> class MyClass { public: MyClass(double); /* ... */ private: std::vector<double> internal_values; };...
10
votes
1answer
235 views

std::tuple_cat but only with unique elements

I have a need for a constexpr function that is very similar to std::tuple_cat, but instead of merging all elements regardless of what they are into one tuple, I need it to add a given element only if ...
-1
votes
1answer
35 views

unordered_map with std::any values cannot any_cast string

I'm new to C++ and I was studying about std::unordered_map and std::any. I've created a sample demo bellow which generates some dummy data and then they get inserted into the map. After that (in the ...
1
vote
2answers
146 views

std::declare_if or other hypothetical ways for discarding member declarations at compile time

SFINAE is quite useful to discard function bodies, but why it can't be used to discard member variables? Is such functionality planned to be added to modern C++ at some point, ever? I tried using ...
2
votes
0answers
42 views

Error when using parameter pack with std::function [duplicate]

I have this code: #include <functional> template<class T, class U> void f(std::function<T(U)> g) {}; int main() { auto h = [](int x) {return x;}; f<int, int>(h); } ...
3
votes
0answers
35 views

std::cout with constexpr on templated function works. Why? [duplicate]

I have this code: #include <iostream> template<class T> constexpr void f(T) { std::cout << ""; } // compiles constexpr void g(int) { std::cout << ""; } // does not compile ...
3
votes
1answer
63 views

“using namespace” for defining global functions

When defining class member functions in the source file, we can use using namespace ns1::ns2::...::nsx to avoid having to fully qualify function names. E.g. // Header file - foo.hpp namespace ns1::...
1
vote
0answers
35 views

boost::stacktrace::frame::name() hangs

I am trying to retrieve a stack trace in my program, and store it for later use (debugging purposes). But the call to boost::stacktrace::frame::name() never returns, and I have no clue why. When I use ...
1
vote
1answer
60 views

Read File At Compile Time (constexpr or other)

Does C++17 offer any way to load/read a file at compile time? More specifically, I want to load a file's content into a const char* or std::string_view, etc. For example: constexpr const char* ...
0
votes
1answer
54 views

Using `std::generate` or `std::fill` to fill an `arma::mat`

I'm trying to write a wrapper around the gmm_full and gmm_diag to have them as mvnorm_distribution. I need this specific setup because I'm dealing with an input file, and would like to have everything ...
-1
votes
0answers
18 views

How can I Declare/define/initialize a static member variable of a inner class of a template class as static member variables of a class? [duplicate]

I have tried to compile following program. template<class A, class B> class Temp{ public: class Inner{ public: static Inner* ptr; }; }; template<class A, class B> ...
2
votes
1answer
98 views

Traversing CRTP inheritance chain for template base classes with non-type template parameters

This is a follow up to the question: Get deepest class in CRTP inheritance chain The following code finds the first template argument which derives from the given instantiation of template CRTP base ...
2
votes
2answers
82 views

Creating a template constrained c++ factory [on hold]

I am fairly new to meta programming and I am trying to create a factory singleton that creates cloneable objects. To this end I have the following code which I have also shared in live coliru The ...
6
votes
2answers
211 views

constexpr and std::cout working on function but not in lambda

Why constexpr does not work with std::cout, but works with printf? #include <iostream> constexpr void f() { std::cout << ""; } //error constexpr void g() { printf(""); } //ok And why std:...
13
votes
3answers
495 views

typeid(“”) != typeid(const char*)

I'm making a C++ library which relies heavily on RTTI (customizable bridge to another language) and is very confused about string literal type. This is a simple test I made to show the problem: std::...
0
votes
3answers
106 views

How to convert std::array<double, 100> to std::array<float, 100> ? (avoiding obvious boilerplate implementation)

Suppose I have an std::array of doubles and would like it converted to floats: std::array<double, 100> d_array{1,2,3}; std::array<float, 100> f_array; <--convert it from d_array; If ...
0
votes
0answers
26 views

What is the syntax for construct array of array using placement new?

i have trying to construct array of array for placement new. i searching internet only manage to found construct an array using placement new. But what if i want array of array instead? i not sure ...
7
votes
1answer
203 views

Template parameter dependant [[nodiscard]]

I have a functional object which is a wrapper around another function: template <typename FuncT> class Wrapper { private: FuncT funcToWrap; public: Wrapper(FuncT ftw) : ...
-1
votes
0answers
40 views

Getting Runtime error for some cases in KNAPSACK problem using RECURSION

The size of weights and values array is n,1 <= N <= 10^4,1<= wi <=100 and 1 <= vi <= 100.We have to find the maximum value such that the weight is less than equal to maximum weight.I ...
2
votes
1answer
67 views

Using variant in Bison and Flex

I've been rewriting a parser from c to c++, and as such I am trying to use variant with my code. However, I am not sure how to integrate it with flex, and I keep getting esoteric error messages. My ...
0
votes
1answer
92 views

What's wrong with my SFINAE?: Testing Supported Operators

This is the code that has the problem: template <typename=std::enable_if_t<supports_v<std::equal_to<>, T>> > bool alreadyValue(const T &value) { return this->value == ...
-2
votes
1answer
66 views

Having trouble with user defined assignment operator for class template [closed]

#include <iostream> #include "Register.h" int main() { // Basic constructors Reg8 r8{ 0xC4 }; Reg16 r16{ 0x1234 }; Reg32 r32{ 0x89ABCDEF }; Reg64 r64{ 0xABBA04200240ABBA }; ...
3
votes
1answer
61 views

Template argument deduction for references as arguments

I am trying to profoundly understand Template Argument Deduction. One point I am not understanding is, how I should apply the rules in the standard here for the types A and P for the following case (...
1
vote
2answers
42 views

How to define a copy constructor for a struct that shares a base class and is defined after the struct being defined

With two structs A and B, that subclass the same Base struct, I want to have copy constructors for A and B in struct A. The forward declaration of B struct B; before struct A is defined is the ...
4
votes
1answer
121 views

Different behaviour of my type_trait in templated/non-templated code

In the following snippet, the has_bar behaves differently in the main and the DoStuff method: In the main method, a_bar == false and b_bar == true. When I execute this, I get 2x "Foo" as output. Why?...
-3
votes
1answer
118 views

Experimenting with algorithms: Is there an inverse to this algorithm?

EDIT - -I renamed the title of this and changed the wording of the question as some have pointed out that this appears to be more of a hashing algorithm than an encryption - decryption algorithm. ...
0
votes
2answers
87 views

CRTP and lifetime extention

My question is how to make lifetime extention work with CRTP. For example, the following code is perfectly valid: struct A { const int& ref; }; struct B { const A& a; }; int main() {...
4
votes
2answers
191 views

Can't compile with static constexpr under pre-C++17 mode

Why doesn't the following minimal example compile with c++11 nor c++14, but compiles in c++17 and c++2a? #include <iostream> #include <limits> #include <vector> // works: // static ...
3
votes
1answer
290 views

Static inline members initialization order

A well known problem in C++ is the static initialization order fiasco. Is it still considered a problem when one use C++17 static inline members? Here an example where a static inline member is used ...
2
votes
2answers
73 views

Unit test to check for `noexcept` property for a C++ method

I have several methods that have to be marked noexcept must not be marked noexcept How to write unit tests that check for the method for being marked noexcept properly? Reason: to ensure that in ...
0
votes
0answers
33 views

Is there a way to capture the parameter names of a method's prototype inside said method or function?

I would like to create macros that define a class with methods at compile time. These methods are proxy methods to a class that is running in another process. The macros define the methods and ...
1
vote
2answers
71 views

How to handle complex header recursion with variants and using

I have (abstractly) 3 headers, two of them containing single variants that calls the other from other. Main structure of those are like: AB.hpp #ifndef AB #define AB //#include "CD.hpp" namespace ...
-1
votes
0answers
21 views

Range based loops for std::multimap with nested STL container (vector) as map value [duplicate]

I'm trying to add a print function to my class that handles a Multimap with nested Vector to hold the values of each key. I've attempted using range based loops, and even using the original context ...
0
votes
1answer
55 views

Fail with dummy_array<int, 6> arr();

The problem is declaration of the array. We can comment the #include <cstdlib> #include <fstream> #include <iostream> #include <vector> #include <list> template <...
1
vote
1answer
44 views

How to apply arithmetic type promotion within a constructor

Hypothetically, If I were creating a point class and I wanted it to deduced the type based on the arguments I would want it to promote the point class to the highest argument. For example: template &...