Showing posts with label cpp11_auto. Show all posts
Showing posts with label cpp11_auto. Show all posts

Aug 21, 2018

[C++] new auto

reference:
https://en.cppreference.com/w/cpp/language/new
ISO 7.6.2.4.2
https://stackoverflow.com/questions/37924996/lambda-with-dynamic-storage-duration

#include <iostream>

using namespace std;


int main()
{
    // movie 'Se7en"
    auto fun = new auto([]() { cout << "oh he didn't know~~" << endl; });
    (*fun)();
}

Dec 14, 2016

[c++][c++17] Declaring non-type template parameters with auto

Declaring non-type template parameters with auto
cppref: aggregate_initialization
int i;
int&& f();
auto            x2a(i);          // decltype(x2a) is int
decltype(auto)  x2d(i);          // decltype(x2d) is int
auto            x3a = i;         // decltype(x3a) is int
decltype(auto)  x3d = i;         // decltype(x3d) is int
auto            x4a = (i);       // decltype(x4a) is int
decltype(auto)  x4d = (i);       // decltype(x4d) is int&
auto            x5a = f();       // decltype(x5a) is int
decltype(auto)  x5d = f();       // decltype(x5d) is int&&
auto            x6a = { 1, 2 };  // decltype(x6a) is std::initializer_list<int>
decltype(auto)  x6d = { 1, 2 };  // error, { 1, 2 } is not an expression
auto*           x7a = &i;        // decltype(x7a) is int*
decltype(auto)* x7d = &i;        // error, declared type is not plain decltype(auto);

int foo(int x) {
    return x;
}
// FAIL TO COMPILE.
// Function id-expressions do not decay to pointers when evaluating with decltype
decltype(auto) v = foo; 


int foo(int x) {
    return x;
}
// int (&) (int)
// Parenthesized function symbol expressions are deduced as a reference to
// the function.
decltype(auto) v = (foo);


// FAIL TO COMPILE
// Lambdas cannot capture references. 
// When y is captured, it captures the referred-to value x. 
// Because captures are const, the captured value ends up as const int. 
// However, decltype(auto) sees the symbol y’s declaration as an int& and deduces the type of v as int&.
// Compilation fails on discarding the const qualifier when trying to assign a const int to an int&.
int x;
int& y = x;
[=] () {
    decltype(auto) v = y;
}();

Nov 27, 2014

[C++11] something about auto... in generic lambda

Generic lambda inconsistency? C++11's six dots
void f(int...);
// equivalent to
void f(int, ...);
Likewise..
[](auto&&...) { return 42; };
// equivalent to
[](auto&& , ...) { return 42; };
This is what allows the funny quirk of 6 periods:
template<typename... Args>
void f(Args&&......) { }
// equivalent to
template<typename... Args>
void f(Args&&..., ...) { }
So, what's the workaround? easy:
[](auto&&... t) { return 42; };

Reference:
https://en.cppreference.com/w/cpp/language/variadic_arguments
In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid.
In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload in SFINAE, exploiting the lowest priority of the ellipsis conversion in overload resolution.

This syntax for variadic arguments was introduced in 1983 C++ without the comma before the ellipsis.

When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma.

For compatibility, C++98 accepts both C++-style f(int n...) and C-style f(int n, ...)

Sep 17, 2014

[C++11] decltype(auto&&) for forwarding.

Use decltype on auto&& parameters to std::forward them.
[](auto&& arg)
{
 RunMe(std::forward<decltype(arg)>(arg));
}(42);
Reasoning:
if arg is l-value,
auto&& -> auto&&& -> auto&
decltype(arg) -> auto&
thus:
forward<auto&>(arg) -> l-value reference

if arg is r-value,
auto&& -> auto&&
decltype(arg)  -> auto&&
thus:
forward<auto&&>(arg) -> auto&& && -> auto&& -> r-value reverence

------------
i.e
int a;
int& b;
int&& c;

decltype(a) -> int
decltype(b) -> int&
decltype(c) -> int&&

decltype((a)) -> int& , i.e l-value reference
decltype((b)) -> int&& -> int& , i.e l-value reference of l-value reference
decltype((c)) -> int&&& -> int& , i.e l-value reference of r-value reference

Jun 9, 2014

[C++11] auto , lambda and proxy object


auto fun = []{}; // 1

function<void()> fun = []{}; // 2

Difference:

1.
Will be on stack.
Will most likely inlined.

2.
Will be on heap or stack, depends on the size of lambda.


Do not use auto with proxy object. e.g :

// b is St14_Bit_reference type, not bool, and point to a dangling ptr.
auto b = vector<bool>({1,0,0})[1];

[C++11] decltype

 
int&& type_(){}                                                                           
int& type__(){}                                                                           
const int type___(){}                                                                     
                                                                                          
int main()                                                                                
{                                                                                         
   int a = 42;                                                                            
   const int ac = 42;                                                                     
   int& b = a;                                                                            
   int&& c = 42;                                                                          
                                                                                          
   decltype(a) a_= 10; // int , name                                                      
   decltype((a)) a__ = a; // int& , expression of l-value, referenced                     
   decltype(b) b_ = a; // int& , name                                                     
   decltype((b)) b__ = a; // int&& => int& , expression of l-value, referenced            
   decltype(c) c_ = 10; // int&& , name                                                   
   decltype((c)) c__ = a; // int&&& => int& , expression of l-value, referenced           

   decltype(ac) ac_ = 10; // const int
                                                                                          
   decltype(type_()) type_ = 10; // int&&, if const int&&, then type is const int&&       
   decltype(type__()) type__ = ac; // int&, if type__() returns const int&, then const int&
   decltype(type___()) type___ = 10; // int , copy by value, ignore c.v                   
                                                                                          
                                                                                          
}                                                                                         
Reference: C++ Type Deduction and Why You Care, Scott Meyers When decltype meets auto