c++17

Const casting empty base class

我的未来我决定 提交于 2019-12-10 13:36:41
问题 Is it undefined behavior to const_cast away an empty base class and call a non const method on it? For example class EmptyBase { public: void bar() { ... } }; class Something : public EmptyBase { public: void foo() const { const_cast<EmptyBase&>(static_cast<const EmptyBase&>(*this)).bar(); } }; I haven't been able to find relevant information in the standards (C++14 and C++17) that answers this.. 回答1: It's not UB in and of itself. You get undefined behavior when you cast away constness and

How to use std::shared_mutex on Vista/Server 2008?

流过昼夜 提交于 2019-12-10 12:52:25
问题 This is a follow-up question to TryAcquireSRWLock* and _WIN32_WINNT As it seems there is a bug in the Windows SDK 8.1 and newer (up to at least the current 10.0.16299.0) making the methods TryAcquireSRWLockShared and TryAcquireSRWLockExclusive available for compilations targeting Windows Vista or Windows Server 2008. This causes applications containing calls to these methods being unable to execute on Windows Vista or Windows Server 2008 since they are ultimately only available starting from

Is the value of expression f() > g(), when f & g modify same global variable undefined or unspecified?

五迷三道 提交于 2019-12-10 12:43:31
问题 UPDATE : As marked by user ecatmur , it's a duplicate of In C99, is f()+g() undefined or merely unspecified? (although the questions asks about C99, but answer is unchanged for C++). And the answer is: unspecified (for both cases). Consider following C++14 code fragment: int i = 0; int x() { i++; return i;} int y() { i++; return i;} bool z = (x() > y()); // unspecified or undefined ? Is the value of z merely unspecified, or is this undefined behavior ? As per my understanding (please correct

Innocent range based for loop not working

不想你离开。 提交于 2019-12-10 12:33:52
问题 The following does not compile: #include <iostream> int main() { int a{},b{},c{},d{}; for (auto& s : {a, b, c, d}) { s = 1; } std::cout << a << std::endl; return 0; } Try it on godbolt Compiler error is: error: assignment of read-only reference 's' Now in my actual case the list is made of member variables on a class. Now, this doesn't work because the expression becomes an initializer_list<int> that actually copies a,b,c, and d - hence also not allowing modification. My question is two-fold:

How can I apply the [[nodiscard]] attribute to a lambda?

让人想犯罪 __ 提交于 2019-12-10 12:32:12
问题 I want to prevent people from calling the lambda without handling the return value. Clang 4.0 refuses everything I've tried, compiling with -std=c++1z: auto x = [&] [[nodiscard]] () { return 1; }; // error: nodiscard attribute cannot be applied to types auto x = [[nodiscard]] [&]() { return 1; }; // error: expected variable name or 'this' in lambda capture list auto x [[nodiscard]] = [&]() { return 1; }; // warning: nodiscard attribute only applies to functions, methods, enums, and classes [

c++17 evaluation order with operator overloading functions

ⅰ亾dé卋堺 提交于 2019-12-10 12:30:36
问题 Regarding this question What are the evaluation order guarantees introduced by C++17? With this specification http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf And this text from the specification Furthermore, we suggest the following additional rule: the order of evaluation of an expression involving an overloaded operator is determined by the order associated with the corresponding built-in operator, not the rules for function calls. Does this mean that these two

Is there any way in C++ to refer to a function template while neither calling it nor supplying its template parameters?

余生长醉 提交于 2019-12-10 11:16:18
问题 Here's the code I would like to work: template <class T> void Foo(T&& param); template <class F> void CallMe(F&& func) { func(42); } int main() { CallMe(Foo); } The compiler chokes when it tries to instantiate CallMe because it doesn't know what I mean by Foo . It works if I write Foo<int> of course, but I'd like to avoid that because in the real code the template parameters can be complex. My current workaround is to use callable objects instead of free functions. For example: class Foo {

Why does an std::any_cast of a passed std::any inside a dlopen'd function raise an error

别等时光非礼了梦想. 提交于 2019-12-10 09:59:47
问题 I am toying around with c++17 and plugins, and I have run into an error that I cannot get around. In the following MWE I can call a local function that takes a std::any , and everything works as expected when I try to read the contents. When I load this exact same function through a plugin (dlopen), it correctly sees the type on the any, but it cannot std::any_cast the contents. Any help would be greatly appreciated in figuring out what is causing this error. Here is my environment, MWE, and

How to make static_assert block re-usable in template classes?

一世执手 提交于 2019-12-10 09:55:50
问题 Say I have a template class that makes multiple static_asserts: template <class T> class Foo { static_assert(!std::is_const<T>::value,""); static_assert(!std::is_reference<T>::value,""); static_assert(!std::is_pointer<T>::value,""); //...<snip>... } Now say I have more template classes that need to make the same asserts. Is there a way to make a static_assert block reusable? A "static_assert function" if you will. 回答1: One thing you can do is build a new trait that is a conjunction of the

Is it possible to get the first type of a parameter pack in a one-liner?

浪子不回头ぞ 提交于 2019-12-10 09:53:38
问题 I have a parameter pack given in a variadic template class and want to extract the first type. Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType should be defined to have the type of the first type in EntityTs . Note, I want to keep the signature of the class template. I know that it would be possible to write template<typename FirstEntityType, typename... OtherEntityTypes> , it is however something that I don't want