c++17

C++17 template deduction guide not used for empty parameter set?

我们两清 提交于 2019-11-29 11:55:00
Consider the following reduced example which can also be viewed at https://godbolt.org/g/Et56cm : #include <utility> template <class T> struct success { T value; constexpr success(T &&v) : value(std::move(v)) { } constexpr success(const T &v) : value(v) { } }; template <> struct success<void> { }; template <class T> success(T /*unused*/)->success<T>; success()->success<void>; int main(void) { auto a = success{5}; // works auto b = success{}; // works auto c = success{"hello"}; // works auto d = success(5); // works auto e = success(); // FAILS! auto f = success("hello"); // works static_assert

Is TR2 Going to be Released in C++17?

旧时模样 提交于 2019-11-29 11:27:27
There is lots of sweet stuff in TR2 . Is that going to be in C++17 ? I understand that TR1 was completed in 2005 and had to wait until C++11 to be standardized. But I also understand that TR2 is already complete? My link to C++17 doesn't mention anything about TR2, but I am hoping... Yakk - Adam Nevraumont Maybe. The point of TR (and now technical specifications) is to allow something to mature independent of the standard iteration process. They can publish a TS, see how it works, see if there are any problems in the implementation and/or use of the feature, and if everything works they can

What changes to C++ made copy initialization work for class with explicit constructor?

百般思念 提交于 2019-11-29 11:26:27
问题 Consider this code: struct X{ explicit X(){} explicit X(const X&){} }; void foo(X a = X()){} int main(){} Using C++14 standard, both GCC 7.1 and clang 4.0 rejects the code, which is what I expected. However, using C++17 ( -std=c++1z ), they both accept the code. What rule changed? For both compilers to exhibit this same behavior, I doubt this to be a bug. But as far as I can tell, the latest draft still says, default argument uses the semantics of copy-initialization 1 . Again, we know that

Parameter with non-deduced type after parameter pack

无人久伴 提交于 2019-11-29 11:23:21
There is different behaviour in clang++ and g++ for the next program: #include <type_traits> #include <utility> template< std::size_t index, typename type > struct ref { type & value; }; template< std::size_t index, typename type > type && get(ref< index, type > const & r) { return std::forward< type >(r.value); } template< typename F, typename ...types, std::size_t ...indices > decltype(auto) apply_inverse(F & f, types &... values, std::index_sequence< indices... >) { struct : ref< indices, types >... {} refs{{values}...}; constexpr std::size_t top = sizeof...(indices) - 1; return std:

inline variable is initialized more than once

六眼飞鱼酱① 提交于 2019-11-29 10:51:24
Im seeing some examples of inline const variable getting initialized (and destructed) 3 times with visual studio 2017. Is this is a bug with the linker ? or is this supposed to happend in some other way ? linker Comdat folding is set to Off. Example Code: #pragma once struct A { A() { static int count = 0; ++count; ASSERT(count == 1); } ~A() { } }; inline const A a = A(); In my solution, I have the assert fire twice (A constructor called 3 times). Inspecting the call stack shows all call stacks are identical and all calls come from dynamic initializer for a(). Now I know for a fact this class

When should I use [[maybe_unused]]?

喜欢而已 提交于 2019-11-29 10:48:35
问题 What is good about using [[maybe_unused]] ? Consider int winmain(int instance, int /*prevInstance*/, const char */*cmdline*/, int show); int winmain(int instance, [[maybe_unused]] int prevInstance, [[maybe_unused]] const char *cmdline, int show); Some might insist that using comments is ugly, because this keyword was made and intended to be used under these circumstances, and I totally agree with it, but the maybe_unused keywords seems a bit too long to me, making the code slightly harder to

Is void{} legal or not?

梦想与她 提交于 2019-11-29 10:47:41
问题 This is a follow-up up of this question. In the comments and in the answer it is said more than once that void{} is neither a valid type-id nor a valid expression . That was fine, it made sense and that was all. Then I came through [7.1.7.4.1/2] ( placeholder type deduction ) of the working draft. There it is said that: [...] - for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type, T is the declared return type and e is the

What does std::includes actually do?

℡╲_俬逩灬. 提交于 2019-11-29 10:43:30
问题 From the standard, std::includes : Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1) . Returns false otherwise. Note: as this is under [alg.set.operations] , the ranges must be sorted Taking this literally, if we let R1=[first1, last1) and R2=[first2, last2) , this is evaluating: ∀a∈R2 a∈R1 However, this is not what is actually being evaluated. For R1={1} and R2={1,1,1} , std::includes(R1, R2) returns false:

Is there any reason to use std::map::emplace() instead of try_emplace() in C++1z?

那年仲夏 提交于 2019-11-29 10:43:22
问题 In C++17, std::map and std::unordered_map got a new member-function template: try_emplace(). This new addition, proposed in n4279, behaves similarly to emplace(), but has the following advantages: try_emplace() does not move from rvalue arguments if the insertion does not happen. This is useful when manipulating maps whose values are move-only types, such as std::unique_ptr. try_emplace() treats the key and the arguments to the mapped_type separately, which makes it somewhat more intuitive

How can I emulate destructuring in C++?

家住魔仙堡 提交于 2019-11-29 10:39:58
问题 In JavaScript ES6, there is a language feature known as destructuring. It exists across many other languages as well. In JavaScript ES6, it looks like this: var animal = { species: 'dog', weight: 23, sound: 'woof' } //Destructuring var {species, sound} = animal //The dog says woof! console.log('The ' + species + ' says ' + sound + '!') What can I do in C++ to get a similar syntax and emulate this kind of functionality? 回答1: For the specific case of std::tuple (or std::pair ) objects, C++