c++17

SFINAE in variadic constructor

谁说胖子不能爱 提交于 2019-12-07 19:51:16
问题 I want to define a generic strong alias type, i.e. a type template<typename T, auto ID = 0> class StrongAlias { T value; }; such that for a type T a StrongAlias<T> can be used in exactly the same way as T , but StrongAlias<T, 0> and StrongAlias<T, 1> are different types that can not be implecitly converted to each other. In order to mimic a T as perfectly as possible, I would like my StrongAlias to have the same constructors as T . This means I would like to do something like the following:

How to represent a variadic templated type based on another group of variadic template arguments in modern C++?

狂风中的少年 提交于 2019-12-07 14:34:08
问题 Suppose I have a following variadic template structure: template <class... T> struct Example {}; Now I want to define a template function: template<class... S> ??? f() { return Example<???> } where the specialization of Example<> is depend on the template parameter S of f . To be more concrete (and simple), now I just want to return Example<int, ...,int> , where the number of int is the size of the parameter pack S . How can it be done in modern C++, i.e. C++11/14/17? More generally, is there

Can a noexcept function still call a function that throws in C++17?

为君一笑 提交于 2019-12-07 13:59:35
问题 In P0012R1, " Make exception specifications be part of the type system ", I see that noexcept is now becoming a part of the function type. I can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false) functions. Will the following code still be valid for C++17? void will_throw() noexcept(false){ throw 0; } void will_not_throw() noexcept(true){ will_throw(); } 回答1: According to cppreference: Note that a noexcept specification on a function is not

::std::initializer_list vs variadic templates

泄露秘密 提交于 2019-12-07 12:29:02
问题 Does passing multiple arguments via ::std::initializer_list offer any advantages over the variadic function template method? In code: template <typename T> void f(::std::initializer_list<T> const); template <typename ...A> void f(A&& ...args); Note that the types A... can be restricted to a single type as well, through SFINAE or static_assert() . The arguments can be iterated through ... 回答1: You can iterate over an std::initializer_list in a way that you cannot over a parameter pack (unless

Order of Evaluation for Fold Expressions

半城伤御伤魂 提交于 2019-12-07 11:59:16
问题 Fold expressions seem to be a nice way to apply a function to each element of a tuple. However, if the applied function has side effects, the order of function invocations might be an important concern. Consider: #include <iostream> template<typename... Ts> void printStuff(Ts... args) { ( ([](auto&& v) { std::cout << v << " "; })(args), ... ); std::cout << '\n'; } int main() { printStuff("hello", 42, 1.5f); // expected output: hello 42 1.5 } This seems to work. But is the order of evaluation

Can static/dynamic/const/reinterpret_cast be used in unevaluated context?

巧了我就是萌 提交于 2019-12-07 11:47:45
问题 I tried to provide structures for checking is A is (choose cast)-castable to B . All four casts would have exact same implementation, expect their names (local-macro-able definitions would be possible, but not necessary). I wrote many check-for operators structures, for example: #include <iostream> #include <type_traits> #include <string> template<class, class, class, class = void> struct is_valid_ternary_operator : std::false_type { }; template<class T, class S, class R> struct is_valid

Can placeholder type in non-type template parameter involve overload resolution of the function passed as a template argument?

你离开我真会死。 提交于 2019-12-07 11:16:06
问题 A follow-up of this question. Assuming placeholder can be used to deduce result type of the function pointer constituting non-type template parameter. Does c++17 allows to perform overload resolution on passed to the template function name - without the knowledge of the result type, that would be needed to perform implicit casting? template <auto(*)(int)> struct Foo { }; int bar(int); float bar(float); int main() { static_cast<void>(Foo<bar>{}); } [gcc] as well as [clang] seem to accept the

Can constexpr-if-else bodies return different types in constexpr auto function?

ぐ巨炮叔叔 提交于 2019-12-07 09:08:50
问题 I'm trying to write a function that maps an enumeration of values to a set of types based on the runtime value of the enumeration. I realize that you cannot return different types based on the runtime value of an enumeration because the compiler wouldn't know how much stack space to allocate. However I'm trying to write this as a constexpr function, using the new if-constexpr functionality to implement this. I'm getting an error from clang complaining that I'm using an illegally specified

C++17 sequencing in assignment: still not implemented in GCC?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 08:01:01
问题 I tried the following code as a naive attempt to implement swapping of R and B bytes in an ABGR word #include <stdio.h> #include <stdint.h> uint32_t ABGR_to_ARGB(uint32_t abgr) { return ((abgr ^= (abgr >> 16) & 0xFF) ^= (abgr & 0xFF) << 16) ^= (abgr >> 16) & 0xFF; } int main() { uint32_t tmp = 0x11223344; printf("%x %x\n", tmp, ABGR_to_ARGB(tmp)); } To my surprise this code "worked" in GCC in C++17 mode - the bytes were swapped http://coliru.stacked-crooked.com/a/43d0fc47f5539746 But it is

Why is a cast operator to std::optional ignored?

自闭症网瘾萝莉.ら 提交于 2019-12-07 05:49:10
问题 This code #include <iostream> #include <optional> struct foo { explicit operator std::optional<int>() { return std::optional<int>( 1 ); } explicit operator int() { return 2; } }; int main() { foo my_foo; std::optional<int> my_opt( my_foo ); std::cout << "constructor: " << my_opt.value() << std::endl; my_opt = static_cast<std::optional<int>>(my_foo); std::cout << "static_cast: " << my_opt.value() << std::endl; } produces the following output constructor: 2 static_cast: 2 in Clang 4.0.0 and in