c++14

Simulating the range-based for loop's begin/end behavior

余生长醉 提交于 2019-12-03 10:44:12
Consider the specification of the range-based for loop's begin-expr and end-expr (N4140 [stmt.ranged]/p1). Given a range __range of type _RangeT , begin-expr and end-expr are determined as follows: if _RangeT is an array type, begin-expr and end-expr are __range and __range + __bound , respectively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of incomplete type, the program is ill-formed; if _RangeT is a class type, the unqualified-id s begin and end are looked up in the scope of class _RangeT as if by class member access lookup (3.4.5), and if either

Is it well-defined to cast xvalues to lvalues for passing to functions?

此生再无相见时 提交于 2019-12-03 10:38:18
Recently I've discovered that sometimes being able to turn rvalues temporarily into lvalues can be useful for me. I've been using the following tool: #include <type_traits> template <typename T> inline constexpr std::remove_reference_t<T> &lvalue(T &&r) noexcept { return static_cast<std::remove_reference_t<T> &>(r); } It's useful when you have to use functions that require lvalues as arguments, but you don't have any interest in what those particular values get changed into. For when you are interested in other output vectors that are not related to the given specific argument. For example,

Pointer to member: works in GCC but not in VS2015

你离开我真会死。 提交于 2019-12-03 10:38:06
I'm trying to implement a "property" system to convert C++ instances into JSON and vice versa. I took a part of the code from Guillaume Racicot's answer in this question ( C++ JSON Serialization ) and simplified it. Here is how I proceed. I have a Property class: template <typename Class, typename T> struct Property { constexpr Property(T Class::* member, const char* name) : m_member(member), m_name(name) {} T Class::* m_member; const char* m_name; }; m_member points to a specific member of Class Let's say I want to define properties for a User class, I would like to be able to proceed like

Avoiding extra move in make_unique/make_shared/emplace/etc for structures that use aggregate initialization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 10:37:19
std::make_unique() (and similar functions) have a little problem : #include <cstdio> #include <memory> using namespace std; struct S { S() { printf("ctor\n"); } ~S() { printf("dtor\n"); } S(S const&) { printf("cctor\n"); } S(S&&) { printf("mctor\n"); } }; S foo() { return S(); } int main() { { printf("--------------- case 1 ---------------\n"); unique_ptr<S> s1 = make_unique<S>( foo() ); } { printf("--------------- case 2 ---------------\n"); unique_ptr<S> s2 { new S( foo() ) }; } } Output: --------------- case 1 --------------- ctor mctor dtor dtor --------------- case 2 --------------- ctor

Are C++14 digit separators allowed in user defined literals?

南笙酒味 提交于 2019-12-03 10:21:06
While clang compiles the following line, g++ 6.1 complains about the digit separator (see live example on Coliru ): auto time = 01'23s; Which compiler, if any, is correct according to the C++14 standard (N3796)? Otherwise, is allowing digit separators (§2.14.2) just an implementation detail in the user-defined literals (§2.14.8) of the <chrono> library (§20.12.5.8)? IMHO it should be not, since these literals are defined on unsigned long long parameters. I remember Howard Hinnant using 10'000s as an example during his CppCon 2016 talk "A <chrono> tutorial" (at about 42 minutes in his talk).

Stripping all qualifiers from a function type

孤街醉人 提交于 2019-12-03 10:06:17
Given a possibly varargs function type with possibly a cv-qualifier-seq and possibly a ref-qualifier , is it possible to write a type trait that strips all the qualifiers without writing 4 * 3 * 2 = 24 partial specializations? template<class T> struct strip_function_qualifiers; template<class R, class... Args> struct strip_function_qualifiers<R(Args...)> { using type = R(Args...); }; template<class R, class... Args> struct strip_function_qualifiers<R(Args..., ...)> { using type = R(Args..., ...); }; template<class R, class... Args> struct strip_function_qualifiers<R(Args...) const> { using

Is it possible to use anonymous classes in C++?

こ雲淡風輕ζ 提交于 2019-12-03 09:36:39
I have seen anonymous classes in C++ code on Quora. It's successfully compiled and run. Code here: #include <iostream> auto func() { class // no name { public: int val; } a; a.val = 5; return a; } int main() { std::cout << func().val << std::endl; return 0; } So, Is it valid in C++? Also, I am curious to know, Is it possible to use anonymous classes in C++? In C++, an anonymous union is a union of this form: union { ... } ; It defines an unnamed object of an unnamed type. Its members are injected in the surrounding scope, so one can refer to them without using an <object>. prefix that

Is there a way to iterate over at most N elements using range-based for loop?

纵然是瞬间 提交于 2019-12-03 09:20:23
I would like to know if there is a nice way to iterate over at most N elements in a container using the range based for loop and/or algorithms from the standard library (that's the whole point, I know I can just use the "old" for loop with a condition). Basically, I'm looking for something that corresponds to this Python code: for i in arr[:N]: print(i) PiotrNycz As I personally would use either this or this answer (+1 for both), just for increasing your knowledge - there are boost adapters you can use. For your case - the sliced seems the most appropriate: #include <boost/range/adaptor/sliced

`std::string` allocations are my current bottleneck - how can I optimize with a custom allocator?

北战南征 提交于 2019-12-03 09:10:43
问题 I'm writing a C++14 JSON library as an exercise and to use it in my personal projects. By using callgrind I've discovered that the current bottleneck during a continuous value creation from string stress test is an std::string dynamic memory allocation. Precisely, the bottleneck is the call to malloc(...) made from std::string::reserve . I've read that many existing JSON libraries such as rapidjson use custom allocators to avoid malloc(...) calls during string memory allocations. I tried to

Determine `constexpr` execution - during compilation or at runtime?

给你一囗甜甜゛ 提交于 2019-12-03 09:02:58
问题 Is there a way to achieve different behaviour of a constexpr function in the compilation phase and at runtime? Consider the following example (using a theoretical feature from D: static if ): constexpr int pow( int base , int exp ) noexcept { static if( std::evaluated_during_translation() ) { auto result = 1; for( int i = 0 ; i < exp ; i++ ) result *= base; return result; } else { // std::evaluated_during_runtime() return std::pow( base , exp ); } } If not, is there a way to restrict