c++14

clang vs gcc - empty generic lambda variadic argument pack

…衆ロ難τιáo~ 提交于 2019-12-22 04:24:06
问题 I think I found another "clang vs gcc" inconsistency between lambdas and callable objects. decltype(l)::operator() should be equivalent to C::operator() , but if variadic pack is left empty in the generic lambda, gcc refuses to compile: 15 : error: no match for call to '(main()::) (int)' l(1); 15 : note: candidate: decltype (((main()::)0u).main()::(x, )) (*)(auto:1&&, auto:2&&, ...) 15 : note: candidate expects 3 arguments, 2 provided 14 : note: candidate: template main():: auto l = [](auto&&

Passing lambda as template parameter to templated by function-pointer function

╄→尐↘猪︶ㄣ 提交于 2019-12-22 03:43:32
问题 Looks like I cannot pass a no-capture lambda as a template parameter to a templated by function-pointer function. Am I doing it the wrong way, or is it impossible? #include <iostream> // Function templated by function pointer template< void(*F)(int) > void fun( int i ) { F(i); } void f1( int i ) { std::cout << i << std::endl; } int main() { void(*f2)( int ) = []( int i ) { std::cout << i << std::endl; }; fun<f1>( 42 ); // THIS WORKS f2( 42 ); // THIS WORKS fun<f2>( 42 ); // THIS DOES NOT WORK

Xcode 5.1 enable C++14

怎甘沉沦 提交于 2019-12-22 03:36:12
问题 Xcode 5.1 using Clang 3.4. And Clang 3.4 supports C++14. However, I've been surfing though all of the Xcode options and don't see a way to enable C++14. I'm trying to enable the relaxed constexpr feature of C++14 回答1: To get this to work, you first set "C++ Language Dialect" to "compiler default". Then in "Other C++ flags" add "-std=c++1y". This will allow Clang++ to compile with c++14 from within Xcode. I tested this with Xcode 5.1.1 using the new user defined literal for basic_string: std:

Explicit destructor call with decltype

回眸只為那壹抹淺笑 提交于 2019-12-22 03:15:26
问题 Consider the following snippet: struct Foo {}; int main() { Foo f; f.~decltype(f)(); // fine with clang, error with gcc f.~decltype(auto)(); // error with both clang and gcc } The rules for an explicit destructor call are handled by the standard grammar with pseudo-destructor-name which is defined as follows: pseudo-destructor-name: nested-name-specifier opt type-name :: ~ type-name nested-name-specifier template simple-template-id :: ~type-name ~ type-name ~ decltype-specifier And: decltype

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

匆匆过客 提交于 2019-12-22 01:33:49
问题 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

Meaning of phrase “constructors do not have names” in the C++ Standard

放肆的年华 提交于 2019-12-22 01:25:08
问题 While trying to understand the phrase "constructors do not have names" in the C++ Standard, it seems like I found an error in clang. Could someone confirm this? VS2015 and gcc rejects this code, and I think they it are is correct. At least, this is the impression I get from §12.1[class.ctor]/2 in N4140: #include <iostream> class A { public: A() { std::cout << "A()" << '\n'; } }; int main() { A::A(); } §12.1[class.ctor]/2 in N4140: A constructor is used to initialize objects of its class type.

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

不想你离开。 提交于 2019-12-22 00:42:05
问题 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

is returning a const std::string really slower than non-const?

 ̄綄美尐妖づ 提交于 2019-12-21 20:55:46
问题 In another question a user made a comment that returning a const std::string loses move construction efficiency and is slower. Is it really true that assigning a string of return of this method: const std::string toJson(const std::string &someText); const std::string jsonString = toJson(someText); ... is really slower than the non-const version: std::string toJson(const std::string &str); std::string jsonString = toJson(someText); And what is the meaning of move-construction efficiency in

Given a set of classes, call the one with matching method parameters

风格不统一 提交于 2019-12-21 20:47:11
问题 I have 2 or more classes which inherit from a single parent. They all have overloaded handle methods, but each class has different parameters for their handle methods. class CommandHandler {}; class FooCommandHandler : public CommandHandler { public: string handle(const FooCommand& c) { return c.getStringSomehow(); } }; class BarCommandHandler : public CommandHandler { public: string handle(const BarCommand& c) { return c.getStringSomeOtherWay(); } string handle(const OtherBarCommand& c) {

C++ The compiler is changing the alignment of my structures. How can I prevent this?

烂漫一生 提交于 2019-12-21 20:46:04
问题 I am writing some code to read bitmap files. Here is the struct I am using to read the bitmap header. See also: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx struct BITMAPFILEHEADER { WORD bfType; // 2 DWORD bfSize; // 6 WORD bfReserved1; // 8 WORD bfReserved2; // 10 DWORD bfOffBits; // 14 }; // should add to 14 bytes If I put the following code in my main function: std::cout << "BITMAPFILEHEADER: " << sizeof(BITMAPFILEHEADER) << std::endl; the program prints