c++14

Using a temporary array as an lvalue

不羁岁月 提交于 2019-12-10 03:16:49
问题 This program is ill-formed: struct X { int i; }; int main() { (X { }).i = 1; } i , a sub-object of the temporary X { } , cannot be used as an lvalue because X { } is an rvalue. However, this silently compiles with GCC 5.2.1 and -Wall : using Y = int[10]; int main() { (Y { })[0] = 1; } If the compiler is correct, then this time, the zeroth element of (Y { }) , which is a subobject of (Y { }) , can be treated as an lvalue. My questions are: Is the second program ill-formed? Why (not), even

Access C++14 lambda captures like struct members

痴心易碎 提交于 2019-12-10 02:58:41
问题 AFAIK, C++11/14 does not allow in-place definition of a new return type while defining a lambda. However, it seems a C++14 lambda capture expression essentially creates an anonymous type with one or more "members" and an operator (). So, why is that the compiler does not allow access to the captured members from outside the lambda. My feeble mind cannot handle the complexities of C++ but does it sound like a reasonable language extension to you? Here is an example. vector<string> words = {

Should I use QScopedPointer or std::unique_ptr?

孤街浪徒 提交于 2019-12-10 02:25:00
问题 I'm starting a new project, using Qt5 and QMAKE_CXXFLAGS += -std=c++1y . I'm not sure whether I should prefer QScopedPointer or std::unique_ptr . I read somewhere that QScopedPointer is not that cool any more. Does QScopedPointer have any features unique_ptr lacks? Are there any features of unique_ptr that I wouldn't want in when replacing QScopedPointer ? Or vice versa? 回答1: QScopedPointer is strictly weaker than unique_ptr as it does not support move semantics. Its functionality is

Buffer filled with different types of data, and strict aliasing

≡放荡痞女 提交于 2019-12-10 02:12:02
问题 According to the standard, it is always undefined behavior in C++ to make, for example, a float* point to the same memory location as a int* , and then read/write from them. In the application I have, there can be a buffer filled with 32-bit integer elements, that are overwritten by 32-bit floating point elements. (It actually contains a representation of an image, that gets transformed in multiple stages by GPU kernels, but there should also be a host implementation that does the same

Why does compiler allow out-of-bounds array access even with constexpr index?

五迷三道 提交于 2019-12-10 01:54:41
问题 For example if we have an std::array and we instantiate an element that is out of bound using constexpr the compiler wouldn't report error: constexpr int EvaluateSpecialArrayIndex(int a) { return a * sizeof(int); } array<int, 5> arr; cout << arr[98] << endl; //compiles fine cout << arr[EvaluateSpecialArrayIndex(4)] << endl; //the same as above Can't we restrict this somehow? 回答1: To ensure that constexpr functions are evaluated at compile time, you must force them to be by making their result

Variable templates and std::cout — order of construction

不问归期 提交于 2019-12-10 01:04:43
问题 It looks like we can safely use std::cout object in constructors of objects with static storage duration as stated in this question. However, I'm not entirely sure that we can safely use them in case of variable templates: #include <iostream> template<class T> T x = T{}; void foo() { class Test { public: Test() { std::cout << "Test::Test\n"; } }; Test t = x<Test>; } int main() { std::cout << "main\n"; } This code crashes in clang (live example) and I'm not sure whether it's a bug or not. 回答1:

“Folding” of template parameter packs in pre C++17: idiomatic approach

别说谁变了你拦得住时间么 提交于 2019-12-09 23:45:57
问题 Fold-ish expressions in C++11 & C++14: idiomatic approach? The accepted answer of the Q&A Variadic template pack expansion makes use of a common pre-C++17 (prior to fold expressions) approach to "folding" of an unexpanded template parameter pack. I've seen a few different variations of this technique; taking the Q&A above as an example: #include <initializer_list> #include <iostream> #include <utility> template <typename T> static void bar(T) {} template <typename... Args> static void foo1

Member detection using void_t

倖福魔咒の 提交于 2019-12-09 17:33:02
问题 For member detection in C++14 I used code based on the example here, but it does no seem to work. A complete example: #include <string> template <typename...> using void_t = void; template <typename, typename = void> class HasMember_substr : public std::false_type {}; template <typename T> class HasMember_substr<T, void_t<typename T::substr>> : public std::true_type {}; template <typename, typename = void> class HasMember_fff : public std::false_type {}; template <typename T> class HasMember

Why doesn't function declared inside other function participate in argument dependent lookup?

心不动则不痛 提交于 2019-12-09 16:55:16
问题 Consider a simple example: template <class T> struct tag { }; int main() { auto foo = [](auto x) -> decltype(bar(x)) { return {}; }; tag<int> bar(tag<int>); bar(tag<int>{}); // <- compiles OK foo(tag<int>{}); // 'bar' was not declared in this scope ?! } tag<int> bar(tag<int>) { return {}; } Both [gcc] and [clang] refuses to compile the code. Is this code ill-formed in some way? 回答1: From unqualified lookup rules ([basic.lookup.unqual]): For the members of a class X , a name used in a member

About ODR-violations and template variables

青春壹個敷衍的年華 提交于 2019-12-09 16:39:31
问题 I know that template functions don't suffer of multiple definitions when linking, like member functions defined inside a class, which are inline by default. Also, constexpr objects have internal linkage, but template variables have external linkage (I mean at namespace scope and for C++14 in both cases). What about? template<class T> constexpr T i_am_odr_safe{}; Does i_am_odr_safe have external or internal linkage in C++14? and is it safe regarding multiple-definitions like function templates