c++17

Under what circumstances will the body under the condition be executed? [duplicate]

牧云@^-^@ 提交于 2019-12-11 09:28:18
问题 This question already has answers here : Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?) (8 answers) Closed last year . Long time ago, while being interviewed, I stumbled across this question: Suppose, somewhere, we have gotten this line of code. Could you provide at least three cases which would cause the body under the condition to be executed? if (a == 3 && a == 4) { /* ... */ } My answers were: It is quite possible that 'a' might be a custom class with overloaded

insert_or_assign is allowing iterator

余生长醉 提交于 2019-12-11 08:51:28
问题 I have this piece of code: auto it = my_map.lower_bound(my_key); The following assert gives me error: static_assert(std::is_same<decltype(it), std::map<K, V>::const_iterator>::value, "Error"); And the following one, is ok: static_assert(std::is_same<decltype(it), std::map<K, V>::iterator>::value, "Error"); Then, the compiler is not giving me a const_iterator . Ok. But here: my_map.insert_or_assign(it, my_key, some_val); even with iterator (not const_iterator ), the function is working. But,

Take a reference if lvalue and make a copy if rvalue i.e. make rvalue persistent

落爺英雄遲暮 提交于 2019-12-11 07:59:31
问题 I'm pretty new in move and lvalue semantics. And I have the impression I'm doing it wrong. Here the code I want to be able to write once FunctContainer is implemented: std::function<double(double)> f = [](double x){return (x * x - 1); }; FunctContainer fc1 = FunctContainer(f); FunctContainer fc2 = FunctContainer([](double x){return (x * x - 1); }); I want to write FunctContainer 's ctors so that the lifetime of the function stored in fc1 is the one of f and the lifetime in fc2 of the

Allocator-aware `std::array`-style container?

别等时光非礼了梦想. 提交于 2019-12-11 07:31:43
问题 I'm writing some code that handles cryptographic secrets, and I've created a custom ZeroedMemory implementation of std::pmr::memory_resource which handles sanitizes memory on deallocation and encapsulates using the magic you have to use to prevent optimizing compilers from eliding away the operation. The idea was to avoid specializing std::array , because the lack of a virtual destructor means that destruction after type erasure would cause memory to be freed without being sanitized.

How can I derive an output type for the template function?

爱⌒轻易说出口 提交于 2019-12-11 07:18:48
问题 I am confused with my template-based multiplication function with two inputs and a type that needs to be derived. How should I derive a type of a function? template<typename T, typename U> DERIVED_TYPE multiply(T t, U u) { return t * u; } Well, I know that auto or decltype(auto) does the work quite well, but I would like the other way if I can, studying is one reason. For example, with Eigen, DERIVED_TYPE multiply(Matrix<int, 2, 3> t, Matrix<double, 3, 4> u) { return t * u; } the DERIVED_TYPE

Compiler error with a fold expression in enable_if_t

╄→гoц情女王★ 提交于 2019-12-11 06:47:31
问题 I have the following code, where I am using a fold expression to evaluate whether all pack parameters are convertible to the first function argument. For some reason it fails to compile on msvc when I make what seems like a very trivial change: #include <type_traits> #define TRY 1 #if TRY == 1 template<typename B, typename... Args, std::enable_if_t<((std::is_convertible_v<Args&, B&> && ...)), bool> = true> void fn(B b, Args...args) {} #else template<typename B, typename... Args, typename =

If statement failing to evaluate condition

断了今生、忘了曾经 提交于 2019-12-11 06:06:03
问题 I have a basic class that containers two enumerators, one for input and one for output. It has two member functions which are both static. The first function is just a static function that returns a value based on the input. It will call the second function which is a constexpr function template that will return the constexpr values. You can see the full class here. class Foo { public: enum Input { INPUT_0 = 0, INPUT_1, INPUT_2 }; enum Output { OUTPUT_0 = 123, OUTPUT_1 = 234, OUTPUT_2 = 345 }

Why does “template argument deduction for class templates” not work on a plain struct?

依然范特西╮ 提交于 2019-12-11 06:02:02
问题 C++17 supports template argument deduction for class templates . Please see www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0091r3.html for detailed background information. However, the code below doesn't work as expected; #include <utility> template<typename T> struct A { T x; }; int main() { auto p = std::pair{ 1, 2 }; // ok, as expected. auto a = A{ 0 }; // // error : no viable constructor or deduction guide // for deduction of template arguments of 'A' // } My compiler is clang 5.0 with

From boost to std::experimental and furthermore c++17

烂漫一生 提交于 2019-12-11 04:59:59
问题 I commonly use boost to implement some features, specially ths boost::filesystem (1.58.0). Also I use std::experimental to string_view (my compiler didn't include it as standard yet - g++ 5.4.0 20160609). Because the boost features I use are aprooved I want to be ready to c++17. Fortunaly I use the following commands in my code: using namespace boost::filesystem; //the only exeption is to boost::filesystem::remove using namespace std::experimental; If I replace the boost line to ' using

How to enumerate all member variables of a class / struct in c++

我只是一个虾纸丫 提交于 2019-12-11 04:17:13
问题 I'm working on some kind of simple reflection for c++ structs where i want to recursivly iterate over all member variables. The code below almost does what i want but my compiler complians: "recursive type or function dependency context too complex" coming form aggregate_arity<MemberType>::size() which is based on Orients aggregate_arity implementation. Example usage case: struct B { SPVStruct; var_t<float2_t, true> f4; }; struct A { SPVStruct; var_t<float2_t, true> f2; var_t<float3_t, true>