c++17

Could reinterpret_cast turn an invalid pointer value into a valid one?

情到浓时终转凉″ 提交于 2020-01-02 07:14:16
问题 Consider this union: union A{ int a; struct{ int b; } c; }; c and a are not layout-compatibles types so it is not possible to read the value of b through a : A x; x.c.b=10; x.a+x.a; //undefined behaviour (UB) Trial 1 For the case below I think that since C++17, I also get an undefined behavior: A x; x.a=10; auto p = &x.a; //(1) x.c.b=12; //(2) *p+*p; //(3) UB Let's consider [basic.type]/3: Every value of pointer type is one of the following: a pointer to an object or function (the pointer is

Passing arguments to another variadic function

两盒软妹~` 提交于 2020-01-02 06:24:08
问题 Is there any way at all for this code to compile and work as intended without resorting to va_list stuff ? #include <iostream> void fct(void) { std::cout << std::endl; } void fct(int index, int indexes...) { std::cout << index << ' '; fct(indexes); //or fct(indexes...); ? } int main(void) { fct(1, 2, 3, 4, 5, 6, 7); return 0; } 回答1: I suspect you have misunderstood the meaning of the signature void fct (int index, int indexes...) I suspect you think that fct() expect a int single value (

Are function attributes inherited?

假如想象 提交于 2020-01-02 02:12:26
问题 If I have a virtual function that carries an attribute [[nodiscard]] virtual bool some_function() = 0; Does that attribute get implicitly applied to overrides of that function? bool some_function() override; Or do I need the attribute again? [[nodiscard]] bool some_function() override; 回答1: I can't see any evidence in the C++17 wording that attributes are inherited by overriding functions. The most relevant section I can find is the rules for overriding: [class.virtual]/2: If a virtual member

Constexpr find for array using c++17

ⅰ亾dé卋堺 提交于 2020-01-01 15:06:53
问题 I am trying to write a constexpr find function that will return the index of a std::array containing a certain value. The function below seems to work OK except when the contained type is const char* : #include <array> constexpr auto name1() { return "name1"; } constexpr auto name2() { return "name2"; } template <class X, class V> constexpr auto find(X& x, V key) { std::size_t i = 0; while(i < x.size()) { if(x[i] == key) return i; ++i; } return i; } int main() { constexpr std::array<const

How to stream std::variant<…,…>

不想你离开。 提交于 2020-01-01 09:04:10
问题 My std::variant contains streamable types: std::variant<int, std::string> a, b; a = 1; b = "hi"; std::cout << a << b << std::endl; Compiling with g++7 with -std=c++1z returns compilation time errors. An excerpt: test.cpp: In function 'int main(int, char**)': test.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::variant<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >') std::cout << a <<

Do const references in structured bindings extend the lifetime of the decomposed object?

馋奶兔 提交于 2020-01-01 07:43:08
问题 Does writing const auto& [a, b] = f(); guarantee extending the lifetime of the object returned from f() , or at least the objects a and b are bound to? Reading through the proposal I don't see anything obvious in the language to make me sure that it does unless it's just covered by something else. However, the following doesn't extend the lifetime of the temporary, so I don't see how it would be covered: const auto& a = std::get<0>(f()); At the top of the paper it seems to suggest that it is

[[maybe_unused]] on enumerator

老子叫甜甜 提交于 2020-01-01 04:37:10
问题 Looking at the specification of the [[maybe_unused]], it states: Appears in the declaration of a class, a typedef­, a variable, a non­static data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused. As this mentions enumerator, I kinda expect it to have a use-case. As the only thing I could come up with is the -Wswitch warning, I tried it with Clang, GCC and MSVC. enum A { B,

What does the standard say about char arrays as template arguments?

别来无恙 提交于 2020-01-01 04:22:27
问题 During my research for an answer for this question I found (I did not know that before) that gcc and clang allow char arrays to be template arguments if they are declared static . E.g. this code compiles with gcc and clang: #include <type_traits> template <int N, const char (&string)[N]> auto foo() { if constexpr (string[0] == 'i') return 0; else return 3.14f; } void bar() { static constexpr char string1[] = "int"; static constexpr char string2[] = "float"; auto i = foo<sizeof(string1),

c++1z dynamic exception specification error

ε祈祈猫儿з 提交于 2020-01-01 04:19:06
问题 I am trying to compile my project with new GCC version 7.2.1 and have a problem with dynamic exception specifications: error: ISO C++1z does not allow dynamic exception specifications MEMORY_ALLOC_OPERATORS(SQLException) The problem is that these errors come from third-party libraries which I do not control. Is there a some way to fix it? As far as I know I can't tell compiler to replace errors with warnings. Using --std=c++14 is not an option because I want to use new features from C++1z.

Clang and GCC disagree in auto specifier for non-type template parameter in a casting C++17

£可爱£侵袭症+ 提交于 2020-01-01 04:04:05
问题 I basically have a class that depends on a non-type template parameter. I defined a casting so an object of non-type template parameter N can convert to another of M . I have a minimal example that can reproduce the situation: template<auto Integral> class Test{ public: typedef decltype(Integral) value_type; static constexpr value_type N = Integral; constexpr Test (const value_type& x = 0); template<auto Integral2> constexpr explicit operator Test<Integral2>() const; private: value_type n; };