c++17

Is it possible to use structured bindings to assign class members?

笑着哭i 提交于 2019-12-02 02:50:48
I'd like to use C++ 17 structured bindings to assign a value to a class member variable, like this: #include <cmath> #include <iostream> struct Result { double value; bool error; }; Result square_root(double input) { return {std::sqrt(input), input < 0}; } struct Calculator { double result_; bool error_; public: void ComputeSquareRoot(double input) { [ result_, error_ ] = square_root(input); } void DisplayResult() { if (error_) std::cout << "Cannot take the square root of a negative number.\n"; else std::cout << "The square root is " << result_ << ".\n"; } }; int main(int argc, char* argv[]) {

Trying to flip the order of bits in std::bitset

断了今生、忘了曾经 提交于 2019-12-02 02:32:59
I'm working on a structure that uses std::bitset and it looks like this: Register.h #pragma once #include <bitset> #include <vector> // used for typedefs of int types namespace vpc { // virtual pc typedef std::int8_t i8; typedef std::int16_t i16; typedef std::int32_t i32; typedef std::int64_t i64; const unsigned int BYTE = 0x08; const unsigned int WORD = 0x10; const unsigned int DWORD = 0x20; const unsigned int QWORD = 0x40; typedef std::bitset<BYTE> Byte; typedef std::bitset<WORD> Word; typedef std::bitset<DWORD> DWord; typedef std::bitset<QWORD> QWord; template<std::uint64_t N = BYTE> struct

Trying to pass a constexpr lambda and use it to explicitly specify returning type

点点圈 提交于 2019-12-02 02:18:06
I would like to use a function and pass a constexpr lambda . However, it only compiles successfully if I let the type be deduced through auto . Explicitly giving the type through -> std::array<event, l()> seems to fail (the first instance). Why is this? template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> std::array<event, l()> { return {}; } // error template <typename Lambda_T> constexpr static auto foo(Lambda_T l) { return std::array<event, (l())>{}; } // OK template <typename Lambda_T> constexpr static auto foo(Lambda_T l) -> decltype(l()) { return {}; } // OK Note that,

Explicit range-v3 decltype evaluates to void?

我与影子孤独终老i 提交于 2019-12-02 01:29:13
问题 I am trying to get an explicit type of a range (I may want to store it as a field in a class in the future). However, for some reason, it evaluates to void ? #include <iostream> #include <set> #include <range/v3/view/transform.hpp> class Alpha { public: int x; }; class Beta : public Alpha { }; class Foo { public: std::set<Alpha*> s; using RangeReturn = decltype(std::declval<std::set<Alpha*>>() | ranges::v3::view::transform(std::function<Beta*(Alpha*)>())); RangeReturn r(); }; Foo::RangeReturn

Generic factory mechanism in C++17

隐身守侯 提交于 2019-12-02 00:29:01
问题 I would like to implement a generic factory mechanism for a set of derived classes that allows me to generically implement not only a factory function to create objects of that class, but also creators of other template classes which take as template arguments one of the derived classes. Ideally a solution would only use C++17 features (no dependencies). Consider this example #include <iostream> #include <string> #include <memory> struct Foo { virtual ~Foo() = default; virtual void hello() =

Generic factory mechanism in C++17

和自甴很熟 提交于 2019-12-02 00:01:25
I would like to implement a generic factory mechanism for a set of derived classes that allows me to generically implement not only a factory function to create objects of that class, but also creators of other template classes which take as template arguments one of the derived classes. Ideally a solution would only use C++17 features (no dependencies). Consider this example #include <iostream> #include <string> #include <memory> struct Foo { virtual ~Foo() = default; virtual void hello() = 0; }; struct FooA: Foo { static constexpr char const* name = "A"; void hello() override { std::cout <<

C++ how to replace constructor switch?

给你一囗甜甜゛ 提交于 2019-12-01 23:39:17
I would like to replace big switch with something more elegant. class Base { public: Base(void*data, int size); virtual void Something() = 0; } class A : public Base { public: A(void*data, int size) : Base(data, size) {} void Something() override; } class B : public Base { public: B(void*data, int size) : Base(data, size) {} void Something() override; } ... { char c = input; switch (c) { case 'a': { A obj(data, size); obj.Something(); break; } case 'b': { B obj(data, size); obj.Something(); break; } ... } } as you see in the example class A and B do not differ from outside. I would like to

boost::combine, range-based for and structured bindings

佐手、 提交于 2019-12-01 23:00:48
问题 Is there a way to make boost::combine work with structured bindings and range-based for (so that identifiers in the structure binding actually point to containers' elements instead of nested tuples of whatever boost::combine uses under the hood)? The following (live example) fails to compile: #include <boost/range/combine.hpp> #include <iostream> int main() { std::vector<int> a{1,2,3}; std::vector<int> b{2,3,4}; for (auto [f, s] : boost::combine(a, b)) { std::cout << f << ' ' << s << std:

if constexpr(condition) as compile-time conditional

拥有回忆 提交于 2019-12-01 20:02:48
I want to use a constexpr bool ( useF in the example below) to enable a feature in the following code. Here, calling A::f() . Additionally, I want to be the alias-template ( a ) to be void in the case I switch off the feature. I tried to use a constexpr if statement, but the body is still being instantiated, which causes a compile error. If I use a wrapper template ( X ), the body is being discarded as I'd expected, but that seems ugly to me. Are there any other ways to do this? constexpr bool useF = false; struct A { static void f() {} }; using a = std::conditional<useF, A, void>::type;

if constexpr(condition) as compile-time conditional

不想你离开。 提交于 2019-12-01 19:50:06
问题 I want to use a constexpr bool ( useF in the example below) to enable a feature in the following code. Here, calling A::f() . Additionally, I want to be the alias-template ( a ) to be void in the case I switch off the feature. I tried to use a constexpr if statement, but the body is still being instantiated, which causes a compile error. If I use a wrapper template ( X ), the body is being discarded as I'd expected, but that seems ugly to me. Are there any other ways to do this? constexpr