c++17

inline variable is initialized more than once

本小妞迷上赌 提交于 2019-12-17 16:47:19
问题 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

Is delete allowed to modify its parameter?

前提是你 提交于 2019-12-17 16:42:04
问题 In an answer, https://stackoverflow.com/a/704568/8157187, there is a quote from Stroustrup: C++ explicitly allows an implementation of delete to zero out an lvalue operand, and I had hoped that implementations would do that, but that idea doesn't seem to have become popular with implementers. However, I failed to find this explicit statement in the standard. There is a part of the current draft standard (N4659), that one may interpret this way: 6.7: When the end of the duration of a region of

Deduction guides and variadic class templates with variadic template constructors - mismatched argument pack lengths

家住魔仙堡 提交于 2019-12-17 14:46:34
问题 Consider the following class definition and deduction guide: template <typename... Ts> struct foo : Ts... { template <typename... Us> foo(Us&&... us) : Ts{us}... { } }; template <typename... Us> foo(Us&&... us) -> foo<Us...>; If I try to instantiate foo with explicit template arguments , the code compiles correctly: foo<bar> a{bar{}}; // ok If I try to instantiate foo through the deduction guide ... foo b{bar{}}; g++7 produces a compiler error: prog.cc: In instantiation of 'foo<Ts>::foo(Us ..

Deducing first template argument with other template parameters defaulted

元气小坏坏 提交于 2019-12-17 09:43:37
问题 Gcc and clang seem to disagree on whether this code should compile or not: #include <type_traits> template <typename Signature, int N = 0> struct MyDelegate { }; template <typename D> struct signature_traits; template <template <typename> class Delegate, typename Signature> struct signature_traits<Delegate<Signature>> { using type = Signature; }; static_assert(std::is_same_v< void(int, int), signature_traits<MyDelegate<void(int, int)>>::type >); See godbolt output here and try it. I'm siding

Is stateful metaprogramming ill-formed (yet)?

独自空忆成欢 提交于 2019-12-17 03:56:29
问题 One of my most beloved/evil inventions I've had the fortune to come across is the constexpr counter, aka stateful metaprogramming. As mentioned in the post, it seems to be legal under C++14, and I'm wondering has anything changed with C++17? The following is an implementation largely based on the post template <int N> struct flag { friend constexpr int adl_flag(flag<N>); constexpr operator int() { return N; } }; template <int N> struct write { friend constexpr int adl_flag(flag<N>) { return N

Is stateful metaprogramming ill-formed (yet)?

99封情书 提交于 2019-12-17 03:56:01
问题 One of my most beloved/evil inventions I've had the fortune to come across is the constexpr counter, aka stateful metaprogramming. As mentioned in the post, it seems to be legal under C++14, and I'm wondering has anything changed with C++17? The following is an implementation largely based on the post template <int N> struct flag { friend constexpr int adl_flag(flag<N>); constexpr operator int() { return N; } }; template <int N> struct write { friend constexpr int adl_flag(flag<N>) { return N

What are template deduction guides and when should we use them?

不打扰是莪最后的温柔 提交于 2019-12-17 03:30:54
问题 The C++17 standard introduces "template deduction guides". I gather they're something to do with the new template argument deduction for constructors introduced in this version of the standard, but I haven't yet seen a simple, FAQ-style explanation of what they are and what they're for. What are template deduction guides in C++17? Why (and when) do we need them? How do I declare them? 回答1: Template deduction guides are patterns associated with a template class that tell the compiler how to

Advantages of auto in template parameters in C++17

若如初见. 提交于 2019-12-17 03:04:34
问题 What are the advantages of auto in template parameters that will (possibly) be introduced with C++17? Is it just a natural extension of auto when I want to instantiate template code? auto v1 = constant<5>; // v1 == 5, decltype(v1) is int auto v2 = constant<true>; // v2 == true, decltype(v2) is bool auto v3 = constant<'a'>; // v3 == 'a', decltype(v3) is char What else do I gain from this language feature? 回答1: The template <auto> feature (P0127R1) was accepted into C++ in the ISO C++ 2016

Template Parameter with implicit array size

孤街醉人 提交于 2019-12-14 03:54:02
问题 Below is a simplified template class that accept an array as a template parameter. However I have to pass also the size of the array as a parameter. I would like to deduce it automatically and to write just: const char *TextArray[] = { "zero", "one", "two" }; Array<TextArray> a; In the actual implementation the class knows the size of TextArray at compile time, this is required (because at compiler time it is checked and paired with other items in the class). I get correctly a compiler error

What is strlen elision?

流过昼夜 提交于 2019-12-14 03:52:34
问题 I can see it listed as one of the modern C++ idioms, but what it is exactly? Is it just a type of copy elision? 回答1: You might know that std::strlen loops over the whole string. This can be inefficient in some cases, as it means that the CPU has to start counting characters, which reduces cache locality for other stuff. But in most cases, the compiler is able to optimize std::strlen and count the number of characters of the string itself instead of making the resulting program do it. This is