c++17

Cast lambda to std::function with parameter pack

你离开我真会死。 提交于 2019-12-09 02:49:43
问题 There are several questions on SO that relate to casting lambdas to std::function s, but I have yet to see one that uses a parameter pack for the argument list. This seems broken on my version of g++ (7.1.1-4), and possibly it's just not supported. So is this legal c++17 (by the standard)? If not, why? #include <functional> template <typename TReturn, typename ... TArgs> void Functor(std::function<TReturn (TArgs...)> f) {} int main(int argc, char * argv[]) { auto x = [] (int a, int b) {

Why string_view instead of generalized container_view<T>?

怎甘沉沦 提交于 2019-12-09 02:27:53
问题 I've found string_view from new C++17 standard a bit redundant. We've got a quite verbose collection of simple mechanisms for passing data to callee, without much overhead and now there is one another which is also specific only to one container type. I don't understand why providing this machinery only for string and not some more generalized type for other containers. One sensible answer is that we've already had these kinds of solutions. For example in C++17 and beyond presentation string

Local reference to std::cout captured by lambda without asking for it

风流意气都作罢 提交于 2019-12-09 02:27:45
问题 Have I lost my mind? Was this always permitted? #include <iostream> int main() { auto& os = std::cout; auto write = []() { os << "what\n"; }; write(); } I'm using: Apple LLVM version 10.0.0 (clang-1000.10.44.4) Target: x86_64-apple-darwin17.7.0 Though also see on Coliru: (live demo) I always thought an empty capture would not capture anything. Indeed, MSDN says: An empty capture clause, [ ], indicates that the body of the lambda expression accesses no variables in the enclosing scope. Further

Why should optional<T&> rebind on assignment?

霸气de小男生 提交于 2019-12-09 02:18:48
问题 There is an ongoing debate about what optional and variant should do with reference types, particularly with regards to assignment. I would like to better understand the debate around this issue. optional<T&> opt; opt = i; opt = j; // should this rebind or do i=j? Currently, the decision is to make optional<T&> ill-formed and make variant::operator= ill-formed if any of the types is a reference type - to sidestep the argument and still give us most of the functionality. What is the argument

int a=1, is a || 1 a constant expression?

有些话、适合烂在心里 提交于 2019-12-09 02:06:54
问题 N4527 5.20[expr.const]p5 A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects: — each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, and — if the object or subobject is of pointer type, it contains the

Make a function accepting an optional to accept a non-optional?

故事扮演 提交于 2019-12-08 22:49:16
问题 I'm trying to write syntactic sugar, in a monad-style, over std::optional . Please consider: template<class T> void f(std::optional<T>) {} As is, this function cannot be called with a non-optional T 1 (e.g. an int ), even though there exists a conversion from T to std::optional<T> 2 . Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload 3 ? 1) f(0) : error: no matching function for call to 'f(int)' and note:

Is cppreference using the term “[Object's] identity” is two different meanings for c++11 and for c++17?

二次信任 提交于 2019-12-08 19:44:13
问题 I thought I've managed to fully understand (with the help of other SO questions, thanks) the C++17's change regarding value categories, but now I've noticed this problem which suggests I don't really understand them. In C++11, there was a "has identity / can be moved from" interpretation of value categories and the definition of what "identity" means is still present in cppreference: has identity: it's possible to determine whether the expression refers to the same entity as another

constructing string from NULL?

那年仲夏 提交于 2019-12-08 19:20:59
问题 After some change of the code-base I came accross this gotcha: #include <string> void test(const std::string& s){ } int main() { test(NULL); return 0; } https://godbolt.org/z/7uJnef This throws an exception. Changing to 'nullptr' helps nothing (still no error or warning). I guess my question is, is there a way to detect or find this error at pre-runtime throughout the sourcecode ? perhaps some compiler warning, etc. (using MSVC VS-2017) I ended up modifying the basic_string template ala.

Is it guaranteed that template template parameter invoke user provided deduction guides

走远了吗. 提交于 2019-12-08 19:16:20
问题 Consider an example: #include <type_traits> #include <string> template <template <class> class TT> //#1 struct Foo { static void foo() { static_assert(std::is_same_v<decltype(TT("abc")), TT<std::string>>); } }; template <class T> struct Bar { Bar(T) {} }; template <class T> Bar(T) -> Bar<std::string>; //#2 int main() { Foo<Bar>::foo(); } [clang] as well as [gcc] both seem to use user provided deduction guides (#2) when deducing the template parameter of template template parameter (#1). Is it

Why is <> required when specifying a template class which has defaults for all its template parameters?

一笑奈何 提交于 2019-12-08 18:58:36
问题 Is there a good reason why <> is required when specifying a template class which has defaults for all its template parameters? e.g. #include <iostream> template<typename T = int> class C { public: T obj = 0; }; int main() { C c1; // Error on almost all compilers (see note below) C<> c2; std::cout << c1.obj << " " << c2.obj << std::endl; return 0; } An example disadvantage of this is that if you have a class which is already used in various places, and you later refactor it to be a class