c++14

Practical limitations on amount of constexpr computation

坚强是说给别人听的谎言 提交于 2019-12-05 10:58:01
As an experiment, I just put together some code to generate a std::array<uint32_t, 256> at compile time. The table contents themselves are a fairly typical CRC lookup table - about the only new thing is the use of constexpr functions to calculate the entries as opposed to putting an autogenerated magic table directly in the source code. Anyway, this exercise got me curious: would there be any practical limitations on the amount of computation a compiler would be willing to do to evaluate a constexpr function or variable definition at compile time? e.g. something like gcc's -ftemplate-depth

Insert/remove type into variadic template list (parameter pack)

依然范特西╮ 提交于 2019-12-05 10:45:14
What is the best way of implementing index-based insertion and deletion of a type in a variadic template type list (parameter pack)? Desired code/behavior: template<typename...> struct List { /* ... */ }; static_assert(is_same < List<int, char, float>::Insert<int, 0>, List<int, int, char, float> >()); static_assert(is_same < List<int, char, float>::Insert<int, 2>, List<int, char, int, float> >()); static_assert(is_same < List<int, char, float>::Remove<0>, List<char, float> >()); static_assert(is_same < List<int, char, float>::Remove<1>, List<int, float> >()); I tried an implementation based on

How to disengage std::experimental::optional?

女生的网名这么多〃 提交于 2019-12-05 10:44:00
With Boost I can create an optional in-place with: boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service)); And disengage it with: work = boost::none; With C++14 / experimental support, I can instead construct an optional in-place with: std::experimental::optional<boost::asio::io_service::work> work; work.emplace(boost::asio::io_service::work(io_service)); But I'm at a loss for how to disengage it... work = std::experimental::nullopt; should disengage work . The library fundamental TS specifies in [optional.nullopt]: The struct nullopt_t is an empty

No matching function std::forward with lambdas

前提是你 提交于 2019-12-05 10:42:00
Consider the following code, inspired from Barry's answer to this question: // Include #include <tuple> #include <utility> #include <iostream> #include <type_traits> // Generic overload rank template <std::size_t N> struct overload_rank : overload_rank<N - 1> { }; // Default overload rank template <> struct overload_rank<0> { }; // Prepend argument to function template <std::size_t N, class F> auto prepend_overload_rank(F&& f) { using rank = overload_rank<N>; return [f = std::forward<F>(f)](rank, auto&&... args) -> decltype(auto) { return std::forward<F>(f)(std::forward<decltype(args)>(args)..

Are there any C++ language obstacles that prevent adopting D ranges?

偶尔善良 提交于 2019-12-05 10:39:33
问题 This is a C++ / D cross-over question. The D programming language has ranges that -in contrast to C++ libraries such as Boost.Range- are not based on iterator pairs. The official C++ Ranges Study Group seems to have been bogged down in nailing a technical specification. Question : does the current C++11 or the upcoming C++14 Standard have any obstacles that prevent adopting D ranges -as well as a suitably rangefied version of <algorithm> - wholesale? I don't know D or its ranges well enough,

C++14 warning: too many template headers for variable (should be 0)

狂风中的少年 提交于 2019-12-05 10:15:12
问题 While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template<T> T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize a<int> . e.g. template<typename T> T a; template<> int a = 1; int main () { std::cout << a<double> << "\n"; // prints 0; OK std::cout << a<int> << "\n"; // prints 0! why not 1? } What is the mystery about this syntax? 回答1: Template arguments can only

Matching variadic non-type templates

烂漫一生 提交于 2019-12-05 09:45:34
问题 Let's say I have two structs, Foo and Bar : template<int...> struct Foo{}; template<unsigned long...> struct Bar{}; I want to create a type trait (call it match_class ) that returns true if I pass two Foo<...> types or two Bar<...> types, but false if I try to mix them: int main() { using f1 = Foo<1, 2, 3>; using f2 = Foo<1>; using b1 = Bar<1, 2, 3>; using b2 = Bar<1>; static_assert(match_class<f1, f2>::value, "Fail"); static_assert(match_class<b1, b2>::value, "Fail"); static_assert(!match

Should trivial default constructor respect default member initializer here?

北城以北 提交于 2019-12-05 09:31:59
Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here ), while gcc 7.2.0 seems so (see here ). Strictly speaking, I think both compilers are right, in that your program exhibits undefined

Is it valid to do explicit template specialisation with auto return 'type' in C++14?

久未见 提交于 2019-12-05 09:06:22
Previous question . I repeat the code from the previous question to make this question self-contained. The code below compiles and does not issue any warnings if it is compiled using gcc 4.8.3. with -std=c++1y . However, it does issue warnings if compiled with -std=c++0x flag. In the context of the previous question it was stated that the code does not compile using gcc 4.9.0. Unfortunately, at present, I do not fully understand how auto is implemented. Thus, I would appreciate if anyone could answer the following questions: 1). Is the code below valid C++ with respect to the C++14 standard? 2

How to make static_assert play nice with SFINAE

会有一股神秘感。 提交于 2019-12-05 08:23:17
Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template<typename T> struct fake_alloc { using value_type = T; }; template<typename T, typename Alloc = fake