c++17

Is there any hope to call a common base class method on a std::variant efficiently?

▼魔方 西西 提交于 2020-01-12 03:11:04
问题 The way std::variant dispatches to different visitor methods when std::visit is called is pretty reasonable when the variant alternatives are completely different types. Essentially a visitor-specific vtable is built at compile-time and after some error checking 1 the appropriate visitor function is looked by indexing the table based the current index() which resolves to something like an indirect jump on most platforms. If the alternatives share a common base class, however, calling a (non

incomplete type for std::any when GMOCKing interface

落爺英雄遲暮 提交于 2020-01-11 12:35:35
问题 I have a very weird compilation problem with this snippet: #include <any> #include <gmock/gmock.h> struct Class { virtual std::any get(int, int) = 0; }; struct MockClass: Class { MOCK_METHOD2(get, std::any(int, int)); }; int foo() { MockClass dd; } Error gcc 9.1.0: /usr/include/c++/9.1.0/type_traits:131:12: error: incomplete type ‘std::is_copy_constructible<testing::internal::ReferenceOrValueWrapper<std::any> >’ used in nested name specifier clang 8.0.0: /usr/bin/../lib64/gcc/x86_64-pc-linux

Invalid explicitly-specified argument for template parameter which is constexpr

社会主义新天地 提交于 2020-01-11 11:32:24
问题 I have a static_loop construct like this template <std::size_t n, typename F> void static_loop(F&& f) { static_assert(n <= 8 && "static loop size should <= 8"); if constexpr (n >= 8) f(std::integral_constant<size_t, n - 8>()); if constexpr (n >= 7) f(std::integral_constant<size_t, n - 7>()); if constexpr (n >= 6) f(std::integral_constant<size_t, n - 6>()); if constexpr (n >= 5) f(std::integral_constant<size_t, n - 5>()); if constexpr (n >= 4) f(std::integral_constant<size_t, n - 4>()); if

c++ variant class member stored by reference

我的梦境 提交于 2020-01-11 11:24:02
问题 I am trying to experiment with std::variant. I am storing an std::variant as a member of a class. In the below code, things work fine if the variant is stored by value, but does not work (for the vector case, and for custom objects too) if the variant is stored by reference. Why is that? #include <variant> #include <vector> #include <iostream> template<typename T> using VectorOrSimple = std::variant<T, std::vector<T>>; struct Print { void operator()(int v) { std::cout << "type = int, value =

c++ variant class member stored by reference

懵懂的女人 提交于 2020-01-11 11:23:11
问题 I am trying to experiment with std::variant. I am storing an std::variant as a member of a class. In the below code, things work fine if the variant is stored by value, but does not work (for the vector case, and for custom objects too) if the variant is stored by reference. Why is that? #include <variant> #include <vector> #include <iostream> template<typename T> using VectorOrSimple = std::variant<T, std::vector<T>>; struct Print { void operator()(int v) { std::cout << "type = int, value =

How can this code be constexpr? (std::chrono)

夙愿已清 提交于 2020-01-11 08:29:27
问题 In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert

Lifetime of object which has vacuous initialization

筅森魡賤 提交于 2020-01-11 06:58:17
问题 Current draft standard says (previous standards have similar wording) in [basic.life/1]: The lifetime of an object or reference is a runtime property of the object or reference. An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its subobjects is initialized by a constructor other than a trivial default constructor. [ Note: Initialization by a trivial copy/move constructor is non-vacuous initialization. — end note ] The lifetime of an

Sum the components of a tuple up by using std::get, std::tuple_size, std::tuple_element

╄→гoц情女王★ 提交于 2020-01-11 03:25:08
问题 I've got a custom class that has a tuple-like interface. Because I want my code to be as generic as possible, I thought that it would be a good idea to base my algorithms on the functions std::get , std::tuple_size , std::tuple_element so you just have to specialize these functions to use my algorithms. Let's call the concept that requires these function specializations Tuple . Now I am trying to sum up the components of a Tuple . The function declaration should be something like this:

Why wasn't yield added to C++0x?

本小妞迷上赌 提交于 2020-01-09 12:59:19
问题 I have been using yield in many of my Python programs, and it really clears up the code in many cases. I blogged about it and it is one of my site's popular pages. C# also offers yield – it is implemented via state-keeping in the caller side, done through an automatically generated class that keeps the state, local variables of the function, etc. I am currently reading about C++0x and its additions; and while reading about the implementation of lambdas in C++0x, I find out that it was done

With std::optional standardizing, can we stop using nullptr in new code and deprecate it? [closed]

两盒软妹~` 提交于 2020-01-07 05:46:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . From time immemorial, when passing pointers to or from functions, we tend to special-case the null pointer: p = get_pointer_to_the_foo(args); if (p == nullptr) { /* foo is inaccessible, do one thing */ } else { /* we can access foo, do something else */ } and this is an