c++17

Auto return type of template and ambiguity

▼魔方 西西 提交于 2021-02-04 12:56:05
问题 I have an overloaded template function: template<typename T1, typename T2> auto overMax(T1 a, T2 b) { std::cout << __FUNCSIG__ << std::endl; return b < a ? a : b; } template<typename RT, typename T1, typename T2> RT overMax(T1 a, T2 b) { std::cout << __FUNCSIG__ << std::endl; return b < a ? a : b; } If I call it like this: auto a = overMax(4, 7.2); // uses first template auto b = overMax<double>(4, 7.2); // uses second template everything works perfect, but auto c = overMax<int>(4, 7.2); //

constexpr begin of a std::array

元气小坏坏 提交于 2021-02-03 23:03:51
问题 I am having trouble understanding why both gcc-8.2.0 and clang-7.0.0 reject the following code (live code here): #include <array> int main() { constexpr std::array<int,3> v{1,2,3}; constexpr auto b = v.begin(); // error: not a constexpr return 0; } with the error error: '(std::array<int, 3>::const_pointer)(& v.std::array<int,3>::_M_elems)' is not a constant expression (constexpr auto b = v.begin();) According to en.cppreference.com, the begin() member function is declared constexpr . Is this

Is there a way to have exceptions work indefinitely?

巧了我就是萌 提交于 2021-01-29 22:38:46
问题 I have been trying to take an input from the user. I want to ensure that the input meets my requirements for the rest of the code for which I have used a try and catch block. However, after only one time catching, it aborts the code. I want to ensure that after catching error it actually goes back to the input function for as many times until the user gives the program a valid input. Is there a way to do that except not using try catch blocks altogether? Here's the code: #include <iostream>

Is there a way to have exceptions work indefinitely?

牧云@^-^@ 提交于 2021-01-29 22:32:43
问题 I have been trying to take an input from the user. I want to ensure that the input meets my requirements for the rest of the code for which I have used a try and catch block. However, after only one time catching, it aborts the code. I want to ensure that after catching error it actually goes back to the input function for as many times until the user gives the program a valid input. Is there a way to do that except not using try catch blocks altogether? Here's the code: #include <iostream>

Multiple Member Constexpr Struc Initialization

霸气de小男生 提交于 2021-01-29 19:17:49
问题 I am new to constexpr, however, I believe that problem I am practicing is a good fit compile-time calculations. This is not for homework or anything, just practicing "Modern" C++. Here is what I have so far: Header (prime_app_lib.hpp): // #pragma once for modern header files (.hpp) #pragma once #include <math.h> static constexpr unsigned max_prime = 10000U; // check out this link for the number of primes: https://primes.utm.edu/howmany.html static constexpr unsigned prime_array_size = 1230U;

Converting a function's parameter - signature from using an `std::function<T>` to a template parameter type

会有一股神秘感。 提交于 2021-01-29 16:36:12
问题 In my existing codebase, I have a non-template class and its constructor has the following declaration signature... struct SomeStruct { double a_; double b_; SomeStruct(double a, double b) : a_{a}, b_{b} {} } class SomeClass { private: SomeStruct fields_; size_t n_; std::function<double(double)> func_; public: SomeClass(SomeStruct fields, size_t n, std::function<double(double)> func) : fields_{fields}, n_{n}, func_{func} {} }; I was using it like this: constexpr double funcA(double x) {

std::ostringstream overwriting initializing string

喜夏-厌秋 提交于 2021-01-29 10:30:52
问题 The following code results in "0004567" on clang++-7 #include <iostream> #include <sstream> using namespace std; int main() { ostringstream oss{"1234567"}; oss << "000"; cout << oss.str() << endl; } Now is this correct STL implementation? I can't think of how is it useful to initialize with a string that will be overwritten... 回答1: @IgorTandetnik gave your a solution - to add std::ios_base::app std::ostringstream constructor argument. However, there is no benefit in passing the initial string

Creating pointers inside a for loop results in pointing to same memory address

烈酒焚心 提交于 2021-01-29 10:05:14
问题 I have an object that I am trying to duplicate for any number of times with some minor changes to that object. I want to store pointers to those duplicated objects in a std::vector . I am using a for loop to try and achieve the result. However, what I notice is that the std::vector<T *> is pointing to the same address after the loop exits. I tried this attempt to duplicate objects with std::string and I see the same effects. Here's my code snippet. int main() { auto name = new std::string(

c++ macro expansion(__VA_ARGS__ item name and value)

泪湿孤枕 提交于 2021-01-29 09:01:02
问题 Is there any trick to achieve the function of the pseudo-code below? THX. template <typename... T1, typename... T2> void fake_fold((T1 t1, T2 t2)...) { (std::make_pair(t1, t2)), ...; // std::pair<std::string, T2>, T1 type is always sd::string } #define FAKE_MACRO(a, b, c) fake_fold(???) void main() { int var1; std::string var2; double var3; FAKE_MACRO(var1, var2, var3); /* macro expansion then pass to fake_fold: fake_fold("var1", var1, "var2", var2, "var3", var3); */ /* fold expression

Avoiding the func(char *) api on embedded

心已入冬 提交于 2021-01-29 08:14:24
问题 Note: I heavily changed my question to be more specific, but I will keep the old question at end of the post, in case it is useful to anyone. New Question I am developing an embedded application which uses the following types to represent strings : string literals(null terminated by default) std::array<char,size> (not null terminated) std::string_view I would like to have a function that accepts all of them in a uniform way. The only problem is that if the input is a string literal I will