c++17

constexpr does not work/apply inside function call

早过忘川 提交于 2019-12-23 03:25:16
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there

constexpr does not work/apply inside function call

廉价感情. 提交于 2019-12-23 03:25:04
问题 I have implemented a constexpr compile-time hash-function, which works fine (i.e. is evaluated at compile-time) if called as constexpr auto hash = CompileTimeHash( "aha" ); but I need to use it in actual code as an argument to a function as in foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr For a specific reason, I cannot use the long version constexpr auto hash = CompileTimeHash( "aha" ); foo( hash ); The compiler (VC++) will not compile-time hash in the short (first) case. Is there

Aliasing boost::variant or std::variant not compilable

好久不见. 提交于 2019-12-23 00:42:23
问题 Currently my library uses boost::optional and boost::variant. Since C++17 is out, I would like to add an option, that it works with the boost and std. So I tested the complete code with boost optional and variant and std optional and variant successful. So I added a header files that is similar to this: #ifdef USE_BOOST #include <boost/optional.hpp> #elif USE_STD #include <optional> #endif namespace Foo { #ifdef USE_BOOST template <typename T> using optional = boost::optional<T> #elif USE_STD

How to create objects in C++ with dynamic alignment requirements?

假装没事ソ 提交于 2019-12-22 18:29:36
问题 What is the correct way in C++ to allocate and use a buffer with dynamically-specified alignment? The use case that I had in mind was Vulkan dynamic uniform buffers (see this previous question which discusses the required process in the abstract), for which a constraint on alignment is given via the minUniformBufferOffsetAlignment property on VkPhysicalDeviceLimits , which is not known at compile time. I initially thought I might be able to use operator new(std::align_val_t) doing something

Why calling shared_from_this calls std::terminate

纵饮孤独 提交于 2019-12-22 12:19:30
问题 Consider this code: class A : public std::enable_shared_from_this<A> { public: std::shared_ptr<A> f() { return shared_from_this(); } }; int main() { A a; std::shared_ptr<A> ptr = a.f(); } This code terminated in Visual Studio 2017. I guess I am doing something wrong here. Can anyone help me with this? I want a shared_ptr on a created by shared_from_this(). 回答1: Because a is not a owned by a shared pointer. From cppreference: It is permitted to call shared_from_this only on a previously shared

Folding a parameter pack of N types into N-1 pairs

醉酒当歌 提交于 2019-12-22 10:52:22
问题 I am trying to fold a parameter pack of N different types into a std::tuple of N-1 std::pairs with respective types. So for example the expression ResolveToTupleOfPairs<void, int, long>::Type tuple; should evaluate to std::tuple<std::pair<void, int>, std::pair<int, long>> tuple; So I am searching for an implementation of the ResolveToTupleOfPairs type to fold the parameter pack as explained. My current implementation follows, but obviously it causes the type to be a tuple of pairs which each

Constexpr counter that works on GCC 8, and is not restricted to namespace scope

馋奶兔 提交于 2019-12-22 10:35:52
问题 I'm trying to learn some arcane stateful template metaprogramming tricks. (Here's why I want to learn it. Unfortunately this library doesn't work on GCC 8 nor on Clang.) The first obvious thing I need is a constexpr counter: /*something*/ constexpr int foo() /*something*/ int main() { constexpr int a = foo(); constexpr int b = foo(); constexpr int c = foo(); static_assert(a == 0 && b == 1 && c == 2); } Preferably it should be a tagged counter, so that I can have several counters at the same

Iterating over tuple in C++17/20 [duplicate]

廉价感情. 提交于 2019-12-22 10:34:52
问题 This question already has answers here : How can you iterate over the elements of an std::tuple? (18 answers) Closed 10 months ago . Does anyone know of a good, clean way to iterate over a tuple in C++17 / 20? Let's say we have a bit of code like this: class Test { public: Test( int x ) : x_(x) {}; void Go() const { std::cout << "Hi!" << x_ << "\n" ; } int x_; }; int main() { std::tuple tplb{ Test{1} , Test{2} , Test{3} }; } How could we iterate through the tuple and call the Go() method on

How to write a template wrapper method for other class member functions?

怎甘沉沦 提交于 2019-12-22 10:18:59
问题 I try to create a templated wrapper for different functions with different parameters. The setup is a class A with the basic implementation of two methods foo and bar . Another class B shall wrap these methods and add new functionality. The solution from the following link works very well for non-class functions: c++11: Templated wrapper function But if I try to invoke methods from another class I get an error. #include <algorithm> #include <functional> #include <iostream> class A { public:

auto parameter type in functions

你说的曾经没有我的故事 提交于 2019-12-22 09:29:22
问题 I would like to know if the standard committee considered expanding the C++14 auto keyword to deduce function template parameter type, as it exists today in generic lambdas. (as can be seen nicely depicted in this answer) Because it works in lambda functions, it should also work in any function. Of course it would be totally redundant with the classic syntax: template< typename T > void f(T param); But being able to write this, for the same result: void f(auto param); I think would allow for