c++17

Can non-type template parameters in c++17 be decltype(auto)?

喜欢而已 提交于 2019-11-29 01:28:51
问题 I discovered that gcc and clang allow to use decltype(auto) in non-type template parameter type clause. E.g.: template <decltype(auto)> struct X {}; int foo ; int main() { X<(foo)> x; static_cast<void>(x); } [live demo gcc] [live demo clang] Is it standard compliant feature or is it some gnu extension? 回答1: This is standard. First, for a non-type template parameter: [temp.param/4] A non-type template-parameter shall have one of the following (optionally cv-qualified) types: ... a type that

why doesn't std::any_cast support implicit conversion?

筅森魡賤 提交于 2019-11-29 00:25:50
问题 Why does std::any_cast throw a std::bad_any_cast exception when an implicit conversion from the actual stored type to the requested type would be possible? For example: std::any a = 10; // holds an int now auto b = std::any_cast<long>(a); // throws bad_any_cast exception Why is this not allowed and is there a workaround to allow an implicit conversion (in case the exact type that std::any holds is unknown)? 回答1: std::any_cast is specified in terms of typeid . To quote cppreference on this:

Should `const` and `constexpr` variables in headers be `inline` to prevent ODR violations?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 21:28:58
Consider the following header and assume it is used in several TUs: static int x = 0; struct A { A() { ++x; printf("%d\n", x); } }; As this question explains, this is an ODR violation and, therefore, UB. Now, there is no ODR violation if our inline function refers to a non- volatile const object and we do not odr-use it within that function (plus the other provisions), so this still works fine in a header: constexpr int x = 1; struct A { A() { printf("%d\n", x); } }; But if we do happen to odr-use it, we are back at square one with UB: constexpr int x = 1; struct A { A() { printf("%p\n", &x);

template argument deduction with strongly-typed enumerations

混江龙づ霸主 提交于 2019-11-28 21:09:32
问题 If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so: enum { Cat, Dog, Horse }; template <int Val, typename T> bool magic(T &t) { return magical_traits<Val>::invoke(t); } and call it as: magic<Cat>(t) as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with: enum class Animal { Cat, Dog, Horse }; template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T

Compile-time reflection in C++1z? [closed]

匆匆过客 提交于 2019-11-28 21:07:27
There is a study group in the C++ standardization committee to provide compile-time reflection in C++1z or after. I would like to know what is exactly the purpose and how powerful the expected tools will be? For example will it be possible to name functions or classes using these tools? struct A {int f() {return 42;}}; struct B {int (std::reflect<A>::member<0>::declname)() {return 43;}}; // equivalent to struct B {int f() {return 43;}}; If it would not be as powerful as this, what the typical use cases will be? Reflection use cases are outlined in N3814: http://www.open-std.org/jtc1/sc22/wg21

Get Apple clang version and corresponding upstream LLVM version

柔情痞子 提交于 2019-11-28 21:00:25
I want to understand which version of clang Apple installed in my macbook, to see with c++11 and/or c++14 features are available. I typed this command: clang --version //----response Apple LLVM version 7.0.0 (clang-700.1.76) Target: x86_64-apple-darwin15.0.0 Thread model: posix But I am not able to understand what (clang-700.1.76) mean. How can I convert this code to a clang version? This is the site where you could check c++ features available in clang version http://clang.llvm.org/cxx_status.html Daniel Frey The (Apple) version number of the compiler is mostly useless, since you also need to

Is it allowed for a compiler to optimize away a local volatile variable?

ⅰ亾dé卋堺 提交于 2019-11-28 20:59:29
Is the compiler allowed to optimize this (according to the C++17 standard): int fn() { volatile int x = 0; return x; } to this? int fn() { return 0; } If yes, why? If not, why not? Here's some thinking about this subject: current compilers compile fn() as a local variable put on the stack, then return it. For example, on x86-64, gcc creates this: mov DWORD PTR [rsp-0x4],0x0 // this is x mov eax,DWORD PTR [rsp-0x4] // eax is the return register ret Now, as far as I know the standard doesn't say that a local volatile variable should be put on the stack. So, this version would be equally good:

Can't stream std::endl with overloaded operator<<() for std::variant

╄→гoц情女王★ 提交于 2019-11-28 20:24:27
This answer describes how to stream a standalone std::variant . However, it doesn't seem to work when std::variant is stored in a std::unordered_map . The following example : #include <iostream> #include <string> #include <variant> #include <complex> #include <unordered_map> // https://stackoverflow.com/a/46893057/8414561 template<typename... Ts> std::ostream& operator<<(std::ostream& os, const std::variant<Ts...>& v) { std::visit([&os](auto&& arg) { os << arg; }, v); return os; } int main() { using namespace std::complex_literals; std::unordered_map<int, std::variant<int, std::string, double,

Why do C++17 structured bindings not use { }?

狂风中的少年 提交于 2019-11-28 20:02:30
I found the original proposal for *C++ structured bindings here . It proposes a way to easily bind multiple return values, i.e.: auto {a, b} = minmax(data); But now I see that everyone points to the C++17/C++1z proposal syntax of auto [a, b] = minmax(data); Now that I learned "lists are written { like, this }" there comes a new list-syntax? Why? What is the problem with curly braces here? This is still under debate. It's difficult to be certain which syntax will be least confusing given how many uses there are for [] and {} already. There's also the risk that "least confusing" and "easiest to

What is Allowed in a constexpr Function?

放肆的年华 提交于 2019-11-28 19:43:09
constexpr functions are not supposed to contain: A definition of a variable of non-literal type But in this answer a lambda is defined in one: https://stackoverflow.com/a/41616651/2642059 template <typename T> constexpr auto make_div(const T quot, const T rem) { return [&]() { decltype(std::div(quot, rem)) result; result.quot = quot; result.rem = rem; return result; }(); } and in my comment I define a div_t in one: How can I Initialize a div_t Object? template <typename T> constexpr decltype(div(T{}, T{})) make_div(const T quot, const T rem) { decltype(div(T{}, T{})) x{}; x.quot = quot; x.rem