c++17

Could an implicit template deduction guide deduce a reference type?

孤者浪人 提交于 2019-12-10 17:18:14
问题 While testing C++17 deduction guide behaviour with gcc7, I found that this example fails: template<class T> struct S{ S(T&& v){} }; int i=10; auto v = S(i); According to what I have read from cpp reference, I thought v should be of type S<int &> . Nevertheless gcc7 does not compile this code complaining that a int& can not be bound to a int && (the universal reference mechanism fails). So my questions are: Should gcc7 have deduced v to be of type S<int&> ? Where are described automatic

Constexpr if with non-template types

白昼怎懂夜的黑 提交于 2019-12-10 17:17:08
问题 #include <iostream> int foo(int x) { if constexpr (std::is_same_v<decltype(x), std::string>) { x = std::string(); } } int main(void) { return 0; } This code doesn't compile on either GCC 7 nor Clang 5: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘int’ in assignment x = std::string(); Since the referenced line is in a constexpr if branch that should evaluate to false , shouldn't the program compile fine? 回答1: The if constexpr specification defines the

C++17 sequencing: post-increment on left side of assignment

折月煮酒 提交于 2019-12-10 17:13:09
问题 The C++17 standard revised the definitions of the order of operations for the C++ language by a rule stating, to the effect: In every simple assignment expression E1=E2 and every compound assignment expression E1@=E2, every value computation and side-effect of E2 is sequenced before every value computation and side effect of E1 However, when compiling the following code in GCC 8.1 with -std=c++17 and -Wall int v[] { 0,1,2,3,4,5,6,7 }; int *p0 = &v[0]; *p0++ = *p0 + 1; cout << "v[0]: " << v[0]

Counting function arguments at compile time

断了今生、忘了曾经 提交于 2019-12-10 16:38:47
问题 I'm trying to count the number of arguments to a function at compile time (I'm wrapping sprintf up in some templates for compile time checks and type safety). I need to check that the number of arguments matches the number of formatting placeholders at compile time. A first pass at this is pretty simple: template <typename... Args> constexpr u32 CountArgs(Args&&... args) { return sizeof...(args); } constexpr u32 CountFormatSpecifiers(c8* format); template <typename... Args> c8* String

flto crash with gcc7.2

旧城冷巷雨未停 提交于 2019-12-10 16:35:57
问题 I have a crash with getline in the following code file. I built gcc7.2 because system updates are not available. Minimal example : #include <iostream> int main(int argc, char *argv[]) { std::string line; while (std::getline(std::cin, line)) { } return 0; } On the following lines, GCC_INSTALL_DIR represents the directory where my own gcc is installed Output : ./a.out a *** Error in `./a.out': free(): invalid pointer: 0x0000000000602200 *** ======= Backtrace: ========= /lib64/libc.so.6(+0x7cfe1

Overload a method in a way that generates a compiler error when called with a temporary

假如想象 提交于 2019-12-10 16:09:56
问题 Perhaps this piece of code will illustrate my intent best: #include <array> template <size_t N> void f(std::array<char, N> arr) { } template <size_t N> void f(std::array<char, N>&& arr) { static_assert(false, "This function may not be called with a temporary."); } f() should compile for lvalues but not for rvalues. This code works with MSVC, but GCC trips on the static_assert even though this overload is never called. So my question is two-fold: how to express my intent properly with modern C

Do scalar members in a union count towards the common initial sequence?

不想你离开。 提交于 2019-12-10 15:57:14
问题 In the union U below, if a or b is the active member, is it defined behavior to access c ? struct A{ int a; }; struct B{ int a; double b; }; union U{ A a; B b; int c; }; In [class.union], the standard defines some rules to make using a union easier ( emphasis mine): [ Note: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence, and if a non-static data member of an object

std::experimental::ostream_joiner and std::pair

谁说胖子不能爱 提交于 2019-12-10 15:35:59
问题 In c++17/g++7, there's finally the long missed ostream_joiner. It enables proper output to ostreams, separating collection elements with infix delimiters. #include <algorithm> #include <experimental/iterator> #include <iostream> #include <iterator> #include <vector> #include <string> using string = std::string; #if 1 struct pair { string first; string second; }; #else using pair = std::pair<string,string>; #endif std::ostream& operator<<(std::ostream& lhs, const pair &p) { return lhs << p

How to declare and define a static member with deduced type?

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:29:49
问题 I need to define a static member (not constexpr) with a complex (many template parameters) type. Therefore it would be desired to have something like this: struct X { static auto x = makeObjectWithComplexType(); }; But it is not C++. So I tried to workaround it, and thought the snippet below would work, but it does not: #include <string> struct X { static auto abc() { return std::string(); } static decltype(abc()) x; }; decltype(abc()) X::x; int main() {} It fails with error: error: use of

C++ : Check if the template type is one of the variadic template types [duplicate]

风格不统一 提交于 2019-12-10 14:23:07
问题 This question already has answers here : Check if a type is passed in variadic template parameter pack (2 answers) Closed 4 years ago . Let's say we have function: template <typename Kind, typename... Kinds> void foo(){...}; What is the simplest way to check if the type 'Kind' is one of the types 'Kinds' in C++ (including C++1z)? 回答1: You could use the following type trait: template <typename...> struct is_one_of { static constexpr bool value = false; }; template <typename F, typename S,