c++14

Get the type of the return value in C++

≡放荡痞女 提交于 2020-01-11 01:01:07
问题 Suppose we have a function f which returns a value of some unknown type (let's call it T ) and takes a value of the type T as an argument (and possibly has some other arguments). How do I get the return type of f in C++14? There is a way to do it if we know the know the argument types (via std::result_of ). Is it possible if we know all the argument types except T ? Example: template <class F> // F is functor with T operator()(T a, T b) class A { // Here I want to do // T some_function(T some

Compiler can't deduce the return type?

会有一股神秘感。 提交于 2020-01-11 00:57:50
问题 I am trying to use the decltype keyword on an auto function: struct Thing { static auto foo() { return 12; } using type_t = decltype(foo()); }; And I get the following error (gcc 7.4): <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' decltype(foo()); ^ <source>:6:25: error: use of 'static auto Thing::foo()' before deduction of 'auto' Why has the compiler not yet deduced the return type? 回答1: Because for class definition, compiler will first determine all

Why can't I move the std::unique_ptr inside lambda in C++14?

眉间皱痕 提交于 2020-01-10 11:58:10
问题 I want to pass a raw pointer inside lambda, but I don't want it to be leaked, if the lambda isn't invoked. It looks like this: void Clean(std::unique_ptr<int>&& list); void f(int* list) { thread_pool.Push([list = std::unique_ptr<int>(list) ] { Clean(std::move(list)); // <-- here is an error. }); } I get an error in Clang 3.7.0: error: binding of reference to type 'unique_ptr<[2 * ...]>' to a value of type 'unique_ptr<[2 * ...]>' drops qualifiers But I don't see any qualifiers at the first

Is it safe to cast an unsigned char* to char*, and treat the dereferenced pointer as if it really points to a char?

拟墨画扇 提交于 2020-01-10 08:49:47
问题 Following the question titled Warning generated due wrong strcmp parameter handling, there seems to be some questions regarding what the Standard actually guarantees regarding value representation of character types. THE QUESTION This looks fine, but does the Standard guarantee that the (1) will always yield true ? char unsigned * p1 = ...; char * p2 = reinterpret_cast<char *> (p1); *p1 == *p2; // (1) 回答1: THIS MIGHT SURPRISE YOU, but there's no such guarantee in the C++11 Standard (N3337),

Lambda Captures C++14

若如初见. 提交于 2020-01-10 01:54:43
问题 I've encountered a notation like: int x = 4; auto y = [&r = x, x = x+1]()->int { r += 2; return x+2; }(); Can you explain this statement? I was a user of C++03 and recently upgraded to C++11. From today I starts C++14 and encountered this snippet. Thanks! 回答1: Thanks @chris for the wikipedia reference. What I found is - Here is nice explanation who don't know about the old lambda Captures of C++11 In C++14: C++11 lambda functions capture variables declared in their outer scope by value-copy

What's the standard/official name for universal references?

坚强是说给别人听的谎言 提交于 2020-01-09 07:13:49
问题 I know that if a variable or parameter is declared to have type T&& for some deduced type T , that variable or parameter is widely called a universal reference . The term universal reference was introduced by Scott Meyers in his original talk “Universal References in C++11”. However, I wonder what's the official/standard term for universal references . 回答1: Overview It is known that since C++11, a parameter of type T&& is called an rvalue reference [ ISO/IEC 14882:2011 §8.3.2/p2 References

Implementation C++14 make_integer_sequence

家住魔仙堡 提交于 2020-01-08 08:52:50
问题 I tried to implement the C++14 alias template make_integer_sequence , which simplifies the creation of the class template integer_sequence. template< class T, T... I> struct integer_sequence { typedef T value_type; static constexpr size_t size() noexcept { return sizeof...(I) ; } }; template< class T, T N> using make_integer_sequence = integer_sequence< T, 0,1,2, ... ,N-1 >; // only for illustration. To implement make_integer_sequence we need a helper structure make_helper . template< class T

Implementation C++14 make_integer_sequence

别来无恙 提交于 2020-01-08 08:50:09
问题 I tried to implement the C++14 alias template make_integer_sequence , which simplifies the creation of the class template integer_sequence. template< class T, T... I> struct integer_sequence { typedef T value_type; static constexpr size_t size() noexcept { return sizeof...(I) ; } }; template< class T, T N> using make_integer_sequence = integer_sequence< T, 0,1,2, ... ,N-1 >; // only for illustration. To implement make_integer_sequence we need a helper structure make_helper . template< class T

How to declare a function template that accepts forwarding reference and returns either a reference or a copy

三世轮回 提交于 2020-01-06 07:26:29
问题 I'm trying to declare a function template that should accept and return a non-const reference when passed an lvalue, but return an RVO-compatible local copy when passed an rvalue: <template ?> ? StringReplace(? str, ? from, ? to); I want template to generate the following signatures: for non-const lvalues std::string& StringReplace(std::string& str, const std::string& from, const std::string& to); for const lvalues std::string StringReplace(const std::string& str, const std::string& from,

lldb shows wierd std::strings (macOS)

感情迁移 提交于 2020-01-06 07:12:49
问题 I have some troubles getting lldb to show me a correct string output in my CEF FileHandler When I debug this application built with clang-5.0.1 (llvm install with brew) with the debugger lldb: [ 96%] Building CXX object gui/executionGraphGui/CMakeFiles/ExecutionGraphGUI.dir/cefapp/FileSchemeHandlerFactory.cpp.o /usr/local/opt/llvm/bin/clang++ -I/Users/gabrielnuetzi/Desktop/ExecutionGraph/gui/executionGraphGui -I/Users/gabrielnuetzi/Desktop/ExecutionGraph/include -I/Users/gabrielnuetzi/Desktop