c++17

Throw exception on missing function overload with std::variant instead of compile time error

笑着哭i 提交于 2020-05-17 07:20:06
问题 This is a follow up to this question Consider the following code #include <variant> int add_(int a, int b){ return a+b; } float add_(float a, float b){ return a+b; } float add_(int a, float b){ return a+b; } float add_(float a, int b){ return a+b; } using Number = std::variant<int, float>; Number add(Number const& lhs, Number const& rhs ){ return std::visit( []( auto& lhs_, auto& rhs_ )->Number { return {add_( lhs_, rhs_ )}; }, lhs, rhs ); } int main(){ Number a = 1.f; Number b = 2.f; Number

Structural binding and type of variables

喜夏-厌秋 提交于 2020-05-17 04:42:04
问题 I would like to ask a question about structural binding and how the variables get their types. Here is the code I compile. struct T { explicit T(int a = 1) { a_ = a; std::cout << "Default" << std::endl; } T(const T& t) { a_ = t.a_; std::cout << "Copy" << std::endl; } int a_; }; int main() { std::tuple<bool, T> bi{true, T(1)}; std::cout << "START" << std::endl; auto& [b, i] = bi; static_assert(std::is_same<decltype(i), T>::value); std::cout << i.a_ << std::endl; i.a_++; std::cout << i.a_ <<

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

血红的双手。 提交于 2020-05-16 08:04:33
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =

C++11 on modern Intel: am I crazy or are non-atomic aligned 64-bit load/store actually atomic?

倖福魔咒の 提交于 2020-05-16 08:04:21
问题 Can I base a mission-critical application on the results of this test, that 100 threads reading a pointer set a billion times by a main thread never see a tear? Any other potential problems doing this besides tearing? Here's a stand-alone demo that compiles with g++ -g tear.cxx -o tear -pthread . #include <atomic> #include <thread> #include <vector> using namespace std; void* pvTearTest; atomic<int> iTears( 0 ); void TearTest( void ) { while (1) { void* pv = (void*) pvTearTest; intptr_t i =

Type conversion from std::complex<MyType> to std::complex<double>

五迷三道 提交于 2020-05-15 08:02:09
问题 I have a class MyType that implements a user-defined arithmetic type. This class provides the following conversion operator struct MyType { ... operator double() { return to_double(); // This converts my type to a double value } ... }; Using this class as follows works fine: double d = MyType(1); However, using this class as type within std::complex, e.g. #include <complex> std::complex<double> c = std::complex<MyType>(1,1); fails with the following compiler error: error: conversion from 'std

Is -Wreturn-std-move clang warning correct in case of objects in the same hierarchy?

穿精又带淫゛_ 提交于 2020-05-15 04:20:52
问题 Consider the following simple code: struct Base { Base() = default; Base(const Base&); Base(Base&&); }; struct Derived : Base { }; Base foo() { Derived derived; return derived; } clang 8.0.0 gives a warning -Wreturn-std-move on it: prog.cc:21:10: warning: local variable 'derived' will be copied despite being returned by name [-Wreturn-std-move] return derived; ^~~~~~~ prog.cc:21:10: note: call 'std::move' explicitly to avoid copying return derived; ^~~~~~~ std::move(derived) But if one called

Why must std::visit have a single return type?

戏子无情 提交于 2020-05-14 18:17:05
问题 While playing around with std::variant and std::visit the following question came up: Consider the following code: using Variant = std::variant<int, float, double>; auto lambda = [](auto&& variant) { std::visit( [](auto&& arg) { using T = std::decay_t<decltype(arg)>; if constexpr (std::is_same_v<T, int>) { std::cout << "int\n"; } else if (std::is_same_v<T, float>) { std::cout << "float\n"; } else { std::cout << "double\n"; } }, variant); }; It works fine as the following examples show: lambda

Why did std::allocator lose member types/functions in C++17?

﹥>﹥吖頭↗ 提交于 2020-05-11 03:49:06
问题 While looking at std::allocator, I see that members: value_type , pointer , const_pointer , reference , const_reference , size_type , difference_type , and rebind have all been deprecated. Allocators will also no longer have the members: address , max_size , construct , or destroy . Why did this happen? Did it have something to do with polymophic allocators? 回答1: If you look at the relevant isocpp paper you can see that the first set you mention is now thought to be better placed in std:

SFINAE : Delete a function with the same prototype

白昼怎懂夜的黑 提交于 2020-05-07 19:00:25
问题 I wonder what is the difference between this code that works : #include <type_traits> #include <iostream> template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>; template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>; template<typename T, is_ref<T> = true> void foo(T&&) { std::cout << "ref" << std::endl; } template<typename T, is_not_ref<T> = true> void foo(T&&) { std::cout << "not ref" << std::endl; } int main() { int a = 0; foo(a)

Constexpr Class taking const references not compiling

旧街凉风 提交于 2020-05-07 07:05:16
问题 I have the following sample code template<class T1, class T2> class Operation { public: constexpr Operation(const T1& lhs, const T2& rhs) noexcept : m_lhs(lhs), m_rhs(rhs) { } private: const T1& m_lhs; const T2& m_rhs; }; int main() { constexpr int a = 3; constexpr int b = 4; constexpr Operation op(a, b); return 0; } Compiling this with cygwin (gcc 8.2) I get error: 'Operation<int, int>{a, b}' is not a constant expression: constexpr Operation op(a, b); With MSVC 2019 it compiles fine, but