#include
void handler(int)
{
exit(42);
}
void _set_handler(int sig)
{
struct sigaction sact;
sact.sa_handler = &handler;
sigaction(sig, &sact, nullptr);
}
template<int... I>
void set_handler(I... signals)
{
[](...){}( (_set_handler(signals),0)... );
}
int main()
{
set_handler(SIGTERM, SIGINT);
}
Showing posts with label cpp_original. Show all posts
Showing posts with label cpp_original. Show all posts
Jun 11, 2015
[c++] setup signal
[c++] extract inner type
#include
using namespace std;
class Fun
{
struct InnerType
{
int i{42};
};
public:
auto run()
{
return InnerType{};
}
auto run2() -> InnerType;
};
template< typename R, typename _>
void extractInnerType( R(_::*)() )
{
cout << R{}.i << endl;
}
template< typename T>
T innerType{};
int main(void)
{
// Blog's question:
Fun f;
decltype(f.run()) d{};
cout << d.i << endl;
//variable template
cout << innerType<decltype(f.run())>.i << endl;
//--- c++98 style
extractInnerType(true ? nullptr : &Fun::run2);
}
Labels:
cpp,
cpp_note,
cpp_original,
cpp_sample,
cpp11,
cpp14
Sep 25, 2014
[C++11] decltype(captured var) inside lambda expression
trap.
We would consider the lambda expression generated below:
equals to:
decltype(local_var) within the lambda expression is the type
of local variable local_var, which is int.
Local type Lambda's decltype(local_var)
is the type of l-value reference to int, which can't bind
to r-value 42.
While considering lambda expression
'is equal to' type Lambda,
that's not exact the case for decltype(local_var) inside lambda expression.
decltype(local_var) is the type of outer scope's local_var,
i.e int.
(transformation not considered)
reference: C++11 ISO14882-2011 , i.e : 5.1.2.18
We would consider the lambda expression generated below:
void fun()
{
static int a;
int local_var = 42;
[&local_var]{
decltype(local_var) var_1 = 42;
}();
}
equals to:
void fun()
{
static int a;
int local_var = 42;
struct Lambda
{
int& local_var;
Lambda(int& _local_var):local_var(_local_var){}
void operator()() const
{
decltype(local_var) var_1 = 42; // won't compile
}
};
Lambda{local_var}();
}
but not quite.decltype(local_var) within the lambda expression is the type
of local variable local_var, which is int.
Local type Lambda's decltype(local_var)
is the type of l-value reference to int, which can't bind
to r-value 42.
While considering lambda expression
'is equal to' type Lambda,
that's not exact the case for decltype(local_var) inside lambda expression.
decltype(local_var) is the type of outer scope's local_var,
i.e int.
(transformation not considered)
reference: C++11 ISO14882-2011 , i.e : 5.1.2.18
Labels:
cpp,
cpp_lambda,
cpp_note,
cpp_original,
cpp11,
cpp11_lambda
Mar 28, 2014
[C++11] Lambda causing bloating template instances
#include <iostream>
#include <functional>
#include <typeinfo>
/*
* 1. In block, every generated lambda has a unique type.
* 2. switch cases , thus goto, has it's own block.
*/
using namespace std;
template<typename T>
void TestBloat(T t)
{
t();
cout << typeid(t).name() << endl;
}
void Test()
{
TestBloat([]{});
TestBloat([]{});
}
int main()
{
cout << "for loop without branch" << endl;
for(int i = 0 ; i <= 4; i++)
{
TestBloat([=]{cout << i << endl;});
TestBloat([=]{cout << i << endl;});
}
int j;
cout << endl;
cout << "for loop with branches" << endl;
for(int i = 0 ; i <= 4; i++)
{
i%2 ? j = i + 1 : j = 0;
if (i < j)
TestBloat([=]{cout << i << endl;});
else
TestBloat([=]{cout << i << endl;});
switch (i)
{
case 1:
TestBloat([=]{cout << i << endl;});
case 2:
TestBloat([=]{cout << i << endl;});
case 3:
TestBloat([=]{cout << i << endl;});
case 4:
TestBloat([=]{cout << i << endl;});
}
}
cout << endl;
cout << "for loop with function calls" << endl;
for(int i = 0 ; i <= 4; i++)
{
Test();
}
}
[C++14] Fun with variable templates
C++14 variable template with default template parameter
template <typename T = double> constexpr T pi = T(3.1415926535897932385); pi<>;C++1y/C++14: Variable Template Specialization
template<typename T> int a = T(); // --1 template<> int a<int> = int(); // --2 a<int>; // will call (2)
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
struct LifeCreator
{
void Vivid()
{
cout << "Hello there!" << endl;
}
LifeCreator GiveMeR()
{
return LifeCreator();
}
void Yell()
{
cout << "This Is Fun!" << endl;
}
};
template<typename T>
struct Talk
{
void operator = (T words)
{
cout << words << endl;
}
};
template<typename T>
T MeaningOfLife = &LifeCreator::Vivid;
template<typename T>
T&& RValueWorld = T().GiveMeR();
template<typename T>
T Translator = T();
int main()
{
(static_cast<LifeCreator*>(0)->*MeaningOfLife<void (LifeCreator::*)()>)();
RValueWorld<LifeCreator>.Yell();
Translator<Talk<string>> = "This", Translator<Talk<string>> = "is" ,
Translator<Talk<string>> = "so", Translator<Talk<string>> = "Auh~~";
}
Mar 27, 2014
[c++14] Possible code bloat by passing lambda expression type to template system
#include <typeinfo>
#include <iostream>
#include <functional>
using namespace std;
using FuncPtr = void(*)(int);
template<typename T>
void Fun(T& t);
template<typename T>
void Fun(T* t);
auto Glambda = [](auto a_){ cout << a_ << endl;};
template<>
void Fun<>(function<void(int)>& function_)
{
function_(42);
}
template<>
void Fun<>(FuncPtr funptr_)
{
funptr_(142);
}
template<>
void Fun<>(decltype((Glambda)) a_)
{
a_(42);
a_(3.14);
a_("it's fun! loving it!");
}
void test()
{
int local;
auto LocalGlambda = [](auto){}; // New type
// Fun(LocalGlambda); Can't instantiate new Fun function type due to no definition.
Fun(Glambda);
// FuncPtr fptr = [local](auto a){cout << a << endl;}; //Not a ptr to func
FuncPtr fptr = [](auto a){cout << a << endl;};
Fun(fptr);
function<void(int)> funcTion = [](auto a){ cout << a << endl;};
function<void(int)> funcTion2 = [local](auto a){ cout << a << endl;};
Fun(funcTion);
Fun(funcTion2);
}
int main()
{
test();
}
Feb 25, 2014
[c++][session id gen][note] Generate Session Id
#include <vector>
#include <random>
#include <limits>
#include <algorithm>
#include <iostream>
class RandomGenerator
{
private:
using Uniform_Dist_ushort_t = std::uniform_int_distribution<unsigned short>;
public:
unsigned long long GenSessionId()
{
std::vector<unsigned int> argv{
ushort_dist(mt),
ushort_dist(mt),
ushort_dist(mt),
ushort_dist(mt)};
std::shuffle(argv.begin(), argv.end(), mt);
sessionID_u seid;
seid.a = argv[0];
seid.b = argv[1];
seid.c = argv[2];
seid.d = argv[3];
return seid.id;
}
private:
union sessionID_u
{
struct{
unsigned short a;
unsigned short b;
unsigned short c;
unsigned short d;
};
unsigned long long id;
};
sessionID_u seid;
private:
std::random_device rd;
std::mt19937 mt{rd()};
Uniform_Dist_ushort_t ushort_dist{0, std::numeric_limits<unsigned short>::max()};
};
int main()
{
std::cout << RandomGenerator().GenSessionId() << std::endl;
}
Labels:
cpp,
cpp_note,
cpp_original,
cpp_random,
cpp_trick,
cpp11
Jan 18, 2014
[C/C++][NOTE] Tail recursive call
Reference:
Tail Call
Tail recursion in C++
Tail Recursion in C++ with multiple recursive function calls
Does C++11 does optimise away tail recursive calls in lambdas?
Tackling C++ Tail Calls
Tail Call
Tail recursion in C++
Tail Recursion in C++ with multiple recursive function calls
Does C++11 does optimise away tail recursive calls in lambdas?
Tackling C++ Tail Calls
https://llvm.org/docs/CodeGenerator.html#tail-call-optimization
* Expand in the callee, not the caller.
* Tail call could be translated to a local loop structure
#include <iostream>
template<typename T, int MeaningOfLife>
struct Fun
{
Fun()
{
std::move(*this).how(MeaningOfLife);
}
T how(int i)&&
{
using namespace std;
cout << "how ";
return std::move(*this).areyou(i);
}
T areyou(int i)&&
{
using namespace std;
cout << "are you?" << endl;
return std::move(*this).how(i);
}
};
int main()
{
Fun<void, 42>();
}
Tail recursive
Use with -O3 and without optimize to see how compiler optimizing the Tail recursive call.
No more stack-overflow for -O3* Expand in the callee, not the caller.
* Tail call could be translated to a local loop structure
Dec 2, 2013
[C++][NOTE][ORIGINAL] const ref to temporary R-Value class without virtual destructor trick
Even though destructor is not virtual,
when out-of-scope happens, base will release reference to R-Value,
thus will release R-Value, i.e Ret_R_Value(); , by calling Derived's destructor.
struct Base
{
~Base()
{
}
};
struct Derived : public Base
{
~Derived()
{
}
}
Derived Ret_R_Value()
{
return Derived();
}
int main()
{
const Base& base = Ret_R_Value();
}
[C++][NOTE][ORIGINAL] Exception & RAII object destructor
Destructors of RAII objects are not invoked if a thrown exception will never be caught,
because in this scenario no exception handler is installed.
Also, if a destructor leaks out an exception while another exception is in flight,
then all bets are off.
because in this scenario no exception handler is installed.
Also, if a destructor leaks out an exception while another exception is in flight,
then all bets are off.
#include <iostream>
using namespace std;
struct Test
{
~Test(){
cout << "ha" << endl;
}
};
void ha()
{
Test t;
throw 0;
}
int main(){
try{
ha();
}
catch(...){
cout << "catch!" << endl;
}
}
Labels:
cpp,
cpp_exception,
cpp_note,
cpp_original,
cpp_raii
Nov 6, 2013
[c++11][NOTE][ORIGINAL] const references in c++ templates
It is just that, for [typedef] aka. [using] in C++11
it's read from right to left, i.e
typedef int* INT;
typedef const INT const_ptr_t;
const_ptr_t is of type :
int* const
This applies to template type system as well.
Which is that:
typedef int& INT;
typedef const INT int_ref_not_really;
There is no const ref, while compiler just dump the constentness
make it to type:
int&
e.g:
template<typename T>
struct Test
{
typedef T& type1;
typedef T&& type2;
typedef const T& type3;
typedef const T&& type4;
};
Test<int>::type1; // int&
Test<int>::type2; // int&&
Test<int>::type3; // int const&
Test<int>::type4; // int const&&
/*
Use specialize template class to deal with input T&, i.e
template<typename T>
struct Test<T&>
{
typedef const T& type1;
};
this will capture (C.V type&). T will be C.V type.
*/
Test<int&>::type1; // int& , int&& collapse to int&
Test<int&>::type2; // int& , int&&& collapse to int&
Test<int&>::type3; // int& , int&& collapse to int& , no const
Test<int&>::type4; // int& , int&&& collapse to int& , no const
Test<int&&>::type1; // int& , int&&& collapse to int&
Test<int&&>::type2; // int&& , int&&&& collapse to int&&
Test<int&&>::type3; // int& , int&&& collapse to int& , no const
Test<int&&>::type4; // int&& , int&&&& collapse to int&& , no const
Test<const int&>::type1; // const int& , const int&& collapse to const int&
Test<const int&>::type2; // const int& , const int&&& collapse to const int&
Test<const int&>::type3; // const int& , const int&& collapse to const int&
Test<const int&>::type4; // const int& , const int&&& collapse to const int&
Reference:
const references in c++ templates
Labels:
cpp,
cpp_note,
cpp_original,
cpp_tmp,
cpp11,
cpp11_tmp,
stackoverflow
Aug 29, 2013
[C++][NOTE][ORIGINAL] Strong typedef
typedef does _not_ creates new type,
but sometimes, we really want _the_ same type but
with different _type_. Ok, it's a bit wordy.
but sometimes, we really want _the_ same type but
with different _type_. Ok, it's a bit wordy.
Jul 29, 2013
[C++11] [Template][NOTE][ORIGINAL] Play around with template meta programming - 1
C++(11) template meta programming dive - 1
1. basic types implement with variadic template
2. light-weight boost::fusion::vector like type container implement with variadic template
3. ref<sequence> , ref<type> dereference function for light-weight type container vector implement with variadic template
This provided some demo code of using variadic template to get rid of traditional type_list, empty type, and use type deduction to extract types in a row.
ideone:
1. basic types implement with variadic template
2. light-weight boost::fusion::vector like type container implement with variadic template
3. ref<sequence> , ref<type> dereference function for light-weight type container vector implement with variadic template
This provided some demo code of using variadic template to get rid of traditional type_list, empty type, and use type deduction to extract types in a row.
ideone:
#include <iostream>
#include <string>
#include <typeinfo>
namespace VS_TEST_SUITE
{
//not necessary in C++11
struct empty
{
};
template<typename T1, typename T2>
struct type_pair
{
using head_t = T1;
using tail_t = T2;
};
template<typename... Ts>
struct type_array
{
};
template<typename T, T VALUE>
struct static_parameter
{
};
template<typename T, T VALUE>
struct static_value : static_parameter<T, VALUE>
{
static const T value = VALUE;
};
//-------------
template<typename... Ts>
struct front;
template<>
struct front<>;
template<typename T1, typename... Ts>
struct front<T1, Ts...>
{
using type = T1;
};
template<typename... Ts>
struct back;
template<>
struct back<>;
template<typename T>
struct back<T>
{
using type = T;
};
template<typename T1, typename... Ts>
struct back<T1, Ts... > : back<Ts...>
{
};
//----------
template<unsigned int N, typename... Ts>
struct type_at
{
using type = empty;
};
template<typename T, typename... Ts>
struct type_at<0, T, Ts...>
{
using type = T;
};
template<unsigned int N, typename T, typename... Ts>
struct type_at<N, T, Ts...>
{
using type = typename type_at<N-1, Ts...>::type;
};
// <int, double>
// at 3
//----------
template<typename... Ts>
struct depth;
template<>
struct depth<> : static_value<unsigned int, 0>
{
};
template<typename T, typename... Ts>
struct depth<T, Ts...> : static_value<unsigned int, depth<Ts...>::value+1>
{
};
//<int,double>
//----------
template<typename T, typename... Ts>
struct type_index : static_value<int, -1>
{
};
template<typename T, typename... Ts>
struct type_index<T, T, Ts...> : static_value<int, 0>
{
};
template<typename T, typename T1, typename... Ts>
struct type_index<T, T1, Ts...> : static_value<int, (type_index<T, Ts...>::value == -1) ? -1 :
type_index<T, Ts...>::value + 1 >
{
};
//<int, double>
//----------
template<typename... Ts>
struct Vector;
template<>
struct Vector<>
{
};
template<typename T, typename... Ts>
struct Vector<T, Ts...> : Vector<Ts...>
{
using value_type = T;
value_type member;
};
//---------
template<typename T, typename... Ts>
T& ref(Vector<T, Ts...>& a_)
{
return a_.member;
}
//---------
template<unsigned int N, template<typename...> class AGG, typename... Ts>
struct ref_traits;
template<template<typename...> class AGG, typename T, typename... Ts>
struct ref_traits<0, AGG, T, Ts...>
{
using value_type = typename AGG<T, Ts...>::value_type;
static value_type& ref(AGG<T, Ts...>& agg)
{
return agg.member;
}
};
template<unsigned int N, template<typename...> class AGG, typename T, typename... Ts>
struct ref_traits<N, AGG, T, Ts...>
{
using next_t = ref_traits<N-1, AGG, Ts...>;
using value_type = typename next_t::value_type;
static value_type& ref(AGG<T, Ts...>& agg)
{
return next_t::ref(agg);
}
};
template<unsigned int N, template<typename...> class AGG, typename... Ts>
inline typename ref_traits<N, AGG, Ts...>::value_type&
ref(AGG<Ts...>& agg)
{
return ref_traits<N, AGG, Ts...>::ref(agg);
}
} //end of namespace VS_TEST_SUITE
int main(){
using namespace std;
using namespace VS_TEST_SUITE;
cout << typeid(back<int, double, std::string>::type).name() << endl; //print string
cout << typeid(front<int, double, std::string>::type).name() << endl; //print int
cout << typeid(type_at<2, double, double, std::string>::type).name() << endl; //print string
cout << typeid(type_at<12, int, double, std::string>::type).name() << endl; //print empty
cout << depth<int, int, int>::value << endl; //print 3
cout << depth<>::value << endl; //print 0
cout << type_index<float, int, double, std::string>::value << endl; //print -1
cout << type_index<double, int, double, std::string>::value << endl; //print 1
Vector<std::string, double> derived;
Vector<double>& base = derived;
Vector<int,std::string, double> agg_1;
ref<int>(agg_1) = 7;
ref<std::string>(agg_1) = "i am a string!";
ref<double>(agg_1) = 20.3;
cout << ref<std::string>(agg_1) << endl;
cout << ref<int>(agg_1) << endl;
cout << ref<double>(agg_1) << endl;
Vector<int, double, std::string> agg_2;
ref<0>(agg_2) = 10;
ref<1>(agg_2) = 3.14;
ref<2>(agg_2) = "i am a string, again!!";
cout << ref<0>(agg_2) << endl;
cout << ref<1>(agg_2) << endl;
cout << ref<2>(agg_2) << endl;
}
Jul 20, 2013
[C++][NOTE][ORIGINAL] Dark Side of C++ - 1
Although compiler will prevent the incomplete type to be
initialized at class level; however, it is allowed to be
used in the function level. Since the function won't be
"initialized" until it's being used, which at that time,
the incomplete type should become complete. But, yes, but,
constructor itself is also a function. And it must be called
while the derived class object being initialized.
Ta da, the incomplete type will be forced to trigger and
an infinite loop has being created.
That's,
Segmentation fault
Welcome to the C++ world.
ideone:
initialized at class level; however, it is allowed to be
used in the function level. Since the function won't be
"initialized" until it's being used, which at that time,
the incomplete type should become complete. But, yes, but,
constructor itself is also a function. And it must be called
while the derived class object being initialized.
Ta da, the incomplete type will be forced to trigger and
an infinite loop has being created.
That's,
Segmentation fault
Welcome to the C++ world.
ideone:
template<typename T>
struct Base
{
void ha(T t);
Base(){
T a;
}
};
struct Derived : Base<Derived>
{
int _a;
Derived() : _a(10) {
}
};
int main(){
Derived derived;
}
Jul 19, 2013
[C++][NOTE][ORIGINAL] Temporary Object Life Time Gotya
C++11 ISO/IEC 14882:2011
Result:
(12.2.4)
There are two contexts in which temporaries are destroyed at a different point than the end of the full expression.
- The first context is when a default constructor is called to initialize an element of an array.
- If the constructor has one or more default arguments, the destruction of every temporary created in a default argument is sequenced before the construction of the next array element, if any.
#include <iostream>
using namespace std;
struct Argument
{
Argument()
{
cout << "Argu Constructor" << endl;
}
~Argument()
{
cout << "Argu Destructor" << endl;
}
};
struct Picacho
{
Picacho(Argument=Argument())
{
cout << "Picacho Constructor" << endl;
}
};
int main(){
Picacho pica[10] = {Picacho()};
}
Result:
Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor Argu Constructor Picacho Constructor Argu Destructor
Jul 18, 2013
[C++][Work][NOTE][ORIGINAL] Bug_Workaround_With_Trait
#include <iostream>
#include <string>
struct TYPE_1
{
std::string result{"type_1"};
std::string get_result() const
{
return result;
}
};
struct TYPE_2
{
std::string result{"type_2"};
std::string get_the_result() const
{
return result;
}
};
struct Trait
{
typedef TYPE_1 type1;
typedef TYPE_2 type2;
};
template<typename T>
struct ToString
{
T inner;
ToString(const T& t) : inner(t){}
operator std::string() const;
};
template<>
ToString<TYPE_1>::operator std::string() const
{
return inner.get_result();
}
template<>
ToString<TYPE_2>::operator std::string() const
{
return inner.get_the_result();
}
template<typename T>
class OUTTER
{
typedef OUTTER This;
template<typename U>
void printf(U u)
{
std::cout << static_cast<const std::string&>(ToString<U>(u)) << std::endl;
}
public:
void run_me()
{
printf(typename T::type1());
printf(typename T::type2());
}
};
int main(){
OUTTER<Trait> outter;
outter.run_me();
}
Jul 17, 2013
[C++][work][NOTE][ORIGINAL] boost::fusion work around idea.
github: boost::fusion workaround
Issues mentioned
Code compiles with : -std=c++11
Online compiled:
stacked-crooked
Issues mentioned
Code compiles with : -std=c++11
Online compiled:
stacked-crooked
#include <iostream>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/bind/placeholders.hpp>
#include <boost/ref.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/include/for_each.hpp>
namespace fusion = boost::fusion;
template<typename T>
class Base
{
protected:
int get_result() const{
return static_cast<const T*>(this)->a;
}
};
class TYPE_1 : public Base<TYPE_1>
{
friend class Base<TYPE_1>;
int a{1};
public:
using Base<TYPE_1>::get_result;
};
class TYPE_2 : public Base<TYPE_2>
{
friend class Base<TYPE_2>;
int a{2};
public:
using Base<TYPE_2>::get_result;
};
class TYPE_3 : public Base<TYPE_3>
{
friend class Base<TYPE_3>;
int a{3};
public:
using Base<TYPE_3>::get_result;
};
struct Stateful
{
int sum_result{0};
};
struct Iterater
{
template<typename T>
void operator()(Stateful& state, const T& t) const{
state.sum_result += t.get_result();
}
};
int main(){
fusion::vector<TYPE_1, TYPE_2, TYPE_3> vec{TYPE_1(), TYPE_2(), TYPE_3()};
Stateful state;
fusion::for_each(vec, boost::bind<void>(Iterater(), boost::ref(state), _1));
std::cout << state.sum_result << std::endl; //print 6
}
Labels:
boost,
boost::fusion,
cpp,
cpp_note,
cpp_original,
cpp_tmp,
cpp11,
sample_code
May 21, 2013
[C++][NOTE][ORIGINAL] Pointer to member function dive
Pointer to member function is really an interesting thing.
Here's the code to observe how pointer-to-member-function structure's offset works.
Here's the code to observe how pointer-to-member-function structure's offset works.
Subscribe to:
Posts (Atom)