c++17

How to define template parameters for a generic lambda argument? [duplicate]

守給你的承諾、 提交于 2019-12-02 07:54:03
问题 This question already has an answer here : Constructing std::function argument from lambda (1 answer) Closed 7 months ago . Explanation: CLion and it's standard compiler give me an error that the "candidate template [is] ignored", when I write a lambda as parameter for a generic function that takes a lambda as argument. This lambda takes a generic type T and returns another unknown type A . The container class that I am writing is supposed to support functional operations like these in Scala

How can I detect that a constuctor is really constexpr, so I can utilize static initialization?

橙三吉。 提交于 2019-12-02 06:11:51
Look at this code: struct NonConstexpr { NonConstexpr() { } }; template <typename T> struct Bar { NonConstexpr nonConstexpr; constexpr Bar() { } }; struct Foo { Bar<void> bar; constexpr Foo() { } }; In this code, Foo 's constructor is tagged as constexpr , but it cannot appear in a constant expression, as it actually fails to satisfy the requirements of this. You can read the details of this in my previous question . My question is: can I detect somehow compile-time, that Foo 's constructor actually won't behave as constexpr ? The reason I ask this, I'd like to detect that a global variable of

VS 2017 program not recognizing “scoped_lock”

只愿长相守 提交于 2019-12-02 05:44:07
I've been having issues using scoped_locked in VS 2017. I believe I traced them back to the <mutex> declaration, copied below. What would be the safest way to disable the #if switch at the beginning to gain use to scoped_lock? Thanks again. #if _HAS_CXX17 // CLASS TEMPLATE scoped_lock template<class... _Mutexes> class scoped_lock { // class with destructor that unlocks mutexes public: explicit scoped_lock(_Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) { // construct and lock _STD lock(_Mtxes...); } explicit scoped_lock(adopt_lock_t, _Mutexes&... _Mtxes) : _MyMutexes(_Mtxes...) { // construct but

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

走远了吗. 提交于 2019-12-02 04:57:51
问题 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

Error C1202 (stack overflow) when recursively computing a templated value or function when using a conditional operator

谁说胖子不能爱 提交于 2019-12-02 04:42:47
I am implementing functionality that provides the opportunity to translate the coordinates of the cells of the game board to the number of this cell. This is what I'm trying (and failing) to make work. #include <cstdint> #include <utility> using UInt32 = std::uint32_t; template<UInt32... s> using IndexSequence = std::integer_sequence<UInt32, s...>; static constexpr UInt32 W = 8; static constexpr UInt32 H = 8; template<UInt32 x1, UInt32 x, UInt32 x2, UInt32 y1, UInt32 y2, UInt32... s> static constexpr auto RegonImpl = (y1 <= y2) ? (x <= x2) ? RegonImpl<x1, x + 1, x2, y1, y2, s..., W * y1 + x> :

incomplete type for std::any when GMOCKing interface

孤街醉人 提交于 2019-12-02 04:38:07
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-gnu/9.1.0/../../../../include/c++/9.1.0/type_traits:132:31: error: no member named 'value' in 'std::is

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

十年热恋 提交于 2019-12-02 04:18:52
问题 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;

How to define template parameters for a generic lambda argument? [duplicate]

故事扮演 提交于 2019-12-02 04:12:21
This question already has an answer here: Constructing std::function argument from lambda 1 answer Explanation: CLion and it's standard compiler give me an error that the "candidate template [is] ignored", when I write a lambda as parameter for a generic function that takes a lambda as argument. This lambda takes a generic type T and returns another unknown type A . The container class that I am writing is supposed to support functional operations like these in Scala or the ones from the Java Stream API. To be exact: The map function makes huge problems. It is implemented as a member function

c++ variant class member stored by reference

懵懂的女人 提交于 2019-12-02 04:11:13
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 = " << v << "\n"; } void operator()(std::vector<int> v) const { std::cout << "type = vector<int>, size =

Comparing constexpr function parameter in constexpr-if condition causes error

孤者浪人 提交于 2019-12-02 04:09:00
I'm trying to compare a function parameter inside a constexpr-if statement. Here is a simple example: constexpr bool test_int(const int i) { if constexpr(i == 5) { return true; } else { return false; } } However, when I compile this with GCC 7 with the following flags: g++-7 -std=c++1z test.cpp -o test I get the following error message: test.cpp: In function 'constexpr bool test_int(int)': test.cpp:3:21: error: 'i' is not a constant expression if constexpr(i == 5) { return true; } However, if I replace test_int with a different function: constexpr bool test_int_no_if(const int i) { return (i =