c++14

Conversion from integral constant expression to null-pointer

折月煮酒 提交于 2019-12-04 23:33:28
Consider following code: #include <memory> void f( std::shared_ptr<int> ) {} int main() { f( 0 ); // compiles fine in gcc and clang f( 1 - 1 ); // compiles fine in gcc, fails in clang constexpr int i = 0; f( i ); // fails to compile in gcc and clang f( i - 0 ); // compiles fine in gcc, fails in clang } why only f( i ) fails to compile, though i should be evaluated as compile time constant with value 0? PS checked with g++ v 5.1.0, it accepts all variants except f(i); in both c++11 and c++14 mode PPS checked with clang 3.7, it rejects all variants except literal 0 in both c++11 and c++14 mode

Avoid if-else branching in string to type dispatching

烈酒焚心 提交于 2019-12-04 23:23:34
Usually when you write a CLI tool which accepts parameter you have to deal with them. Most of the time you want to switch between behaviours based on the value of an argument. The following is a common use case, where the program accepts a type and then prints something based on that type. I am using Boost to pre-process and auto generate the whole if-else branches. This is very nice in terms of maintainability as I only need to update a define when I introduce a new type. On the other hand it is quite far from being modern and elegant. I thought about using better-enums to avoid using the if

how is this lambda with an empty capture list able to refer to reaching-scope name?

邮差的信 提交于 2019-12-04 23:22:00
问题 In the C++14 standard § 5.1.2/12 it shows an example of a lambda expression that apparently seems to be able to refer to a reaching scope's variable x , even though: the capture list is empty, i.e. no capture-default the comment says that it "does not capture x " Here's the example: void f(int, const int (&)[2] = {}) { } // #1 void test() { const int x = 17; auto g = [](auto a) { f(x); // OK: calls #1, does not capture x }; } See that it does compile. It seems to hinge on x being const ; if

C++1y/C++14: Assignment to object outside its lifetime is not allowed in a constant expression?

青春壹個敷衍的年華 提交于 2019-12-04 23:02:19
Is the following C++14/C++1y program ill-formed according to the current draft? #include <cstddef> template<typename T, size_t n> struct literal_array { T data[n]; }; template<typename T, size_t n, size_t m> constexpr literal_array<T, n+m> operator+(literal_array<T, n> a, literal_array<T, m> b) { literal_array<T, n+m> x; for (size_t i = 0; i < n; i++) x.data[i] = a.data[i]; for (size_t i = 0; i < m; i++) x.data[n+i] = b.data[i]; return x; } int main() { constexpr literal_array<int, 3> a = { 1, 2, 3 }; constexpr literal_array<int, 2> b = { 4, 5 }; constexpr auto c = a + b; } Clang trunk (at

C++1y no viable conversion from std::bind to std::function

纵然是瞬间 提交于 2019-12-04 22:31:56
I am trying to store a forward function into std::function . If I use std::bind , I get error message like no viable conversion from ... . If I use lambda, it compile okay. Here is sample code #include <functional> template<typename Handler>void func1(int a, Handler&& handler) {} template<typename Handler>void func2(Handler&& handler) { // this line compile fine std::function<void ()> funcA = [handler = std::move(handler)]() { func1(1, std::move(handler)); }; // this line got compile error std::function<void ()> funcB = std::bind(func1<Handler>, 1, std::move(handler)); } int main() { func2(

Dereferencing a function with default arguments - C++14 vs C++11

岁酱吖の 提交于 2019-12-04 22:30:43
Following code can't be compiled with g++ version 5.4.0 with option -std=c++1y : void f(int=0) ; int main() { f(); // ok (*f)(2);// ok (*f)();// ok c++11; error with c++14: too few arguments to function return 0; } The function declared to have default argument, so what is wrong here? thanks for help. And why does g++ -c -std=c++11 compile? Accepting (*f)() as valid is a GCC bug. The letter of the standard indicates that using a function name with unary * should cause the function name to decay into a pointer. The pointer should then be dereferenced to obtain the functions address for the call

Variable templates and std::cout — order of construction

南笙酒味 提交于 2019-12-04 22:24:08
It looks like we can safely use std::cout object in constructors of objects with static storage duration as stated in this question . However, I'm not entirely sure that we can safely use them in case of variable templates: #include <iostream> template<class T> T x = T{}; void foo() { class Test { public: Test() { std::cout << "Test::Test\n"; } }; Test t = x<Test>; } int main() { std::cout << "main\n"; } This code crashes in clang ( live example ) and I'm not sure whether it's a bug or not. As explained in that question, one effect of #include <iostream> is the equivalent of defining a global

Template friend function and return type deduction

偶尔善良 提交于 2019-12-04 19:05:56
问题 Note: This question is really close to Return type deduction for in-class friend functions, but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code compiles: #include <iostream> template<class T> class MyClass { public: MyClass(T const& a) : impl(a) {} template<class T0, class T1> friend auto // requires operator+(T0,T1) exists operator+(MyClass<T0> const& a, MyClass<T1> const& b) { return MyClass<decltype(a

“No match for operator-” error on simple iterator difference

做~自己de王妃 提交于 2019-12-04 18:53:50
Here is my code: #include <set> #include <iostream> using namespace std; int main(){ set<int> st; st.insert(1); int x = st.find(1) - st.begin(); return 0; } I am getting error: no match for 'operator-' in 'st.std::set<_Key, _Compare, _Alloc>::find [with _Key = int, _Compare = std::less<int>, _Alloc = std::allocator<int>](((const int&)((const int*)(&1)))) - st.std::set<_Key, _Compare, _Alloc>::begin [with _Key = int, _Compare = std::less<int>, _Alloc = std::allocator<int>]()' . I am not able to figure out how did iterator difference stopped working all of a sudden! Am I missing something here?

When and how to use a template literal operator?

白昼怎懂夜的黑 提交于 2019-12-04 18:23:49
问题 On cppreference there is a mentioning that one can have templated user-literal operators, with some restrictions: If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a non-type template parameter pack with element type char , such as template <char...> double operator "" _x(); So I wrote one like in the code below: template <char...> double operator "" _x() { return .42; } int main() { 10_x; // empty template list