Skip to content

Instantly share code, notes, and snippets.

@shajeen
Last active August 2, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shajeen/fc48aa5b1bcd071e1234f327034d0823 to your computer and use it in GitHub Desktop.
Save shajeen/fc48aa5b1bcd071e1234f327034d0823 to your computer and use it in GitHub Desktop.
c++11 concepts
// cpp11.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <memory>
#include <array>
#include <vector>
#include <string>
#include <algorithm>
// cpp version
void _1()
{
std::cout << "-- cppversion --" << std::endl;
std::cout << __cplusplus << std::endl;
}
// auto
void _2()
{
std::cout << "\n-- auto --" << std::endl;
// it is type of template
auto iValue = 0; // so it become int
auto fValue = 0.0; // so it become float
auto p = std::make_shared<long>(506);
std::cout << typeid(iValue).name() << " " << iValue << std::endl;
std::cout << typeid(fValue).name() << " " << fValue << std::endl;
std::cout << typeid(p).name() << " " << *p << std::endl;
}
// Range for statement
void _3()
{
std::cout << "\n-- Range of Statement -- " << std::endl;
// creating a array
std::array<int, 9> localArray{ 1,2,3,4,5,6,7,8,9 };
// range base loop
for (const auto &iValue : localArray) {
std::cout << iValue << ",";
}
std::cout << std::endl;
// exmaple 2
for (const auto iValue : {
9,8,7,6,5,4,3,2,1
}) {
std::cout << iValue << ",";
}
}
// default and delete
void _4()
{
std::cout << "\n\n-- default and delete --" << std::endl;
// creating a class
class Example
{
public:
Example() = default; // make default constuctor
~Example() = default; // make default destructor
Example(const Example&) = delete; // make copy constructor disable
Example& operator=(const Example&) = delete; // make disable
};
std::cout << "constructor -> default \ndestructor -> default \ncopy constructor -> disable \ncopy operator -> disable" << std::endl;
}
// enum
void _5()
{
std::cout << "\n-- enum --" << std::endl;
// creating of enum class
enum class Color : std::int8_t
{ White, Yellow, Black, Green };
const constexpr Color myColor = Color::Yellow;
// it is not possible to
// int aColor = Color::Green;
std::cout << "myColor == Color::Yellow : " << std::boolalpha << (myColor == Color::Yellow) << std::endl;
}
// constexpr
void _6()
{
std::cout << "\n-- constexpr --" << std::endl;
// creating struct
struct Point
{
int iXValue, iYValue;
constexpr Point(int iX, int iY) : iXValue{ iX }, iYValue{ iY } {}
};
constexpr Point point(100, 200);
std::cout << "created object from constexpr, " << point.iXValue << " " << point.iYValue << std::endl;
}
// decltype
void _7()
{
std::cout << "\n-- decltype --" << std::endl;
// creating base container objects
const int iX = 0;
const float fX = 0.0;
const long lX = 00;
const double dX = 0.0;
std::cout << "typeid(decltype(iX)).name() => " << typeid(decltype(iX)).name() << std::endl;
std::cout << "typeid(decltype(fX)).name() => " << typeid(decltype(fX)).name() << std::endl;
std::cout << "typeid(decltype(lX)).name() => " << typeid(decltype(lX)).name() << std::endl;
std::cout << "typeid(decltype(dX)).name() => " << typeid(decltype(dX)).name() << std::endl;
}
// initializer_list
void _8()
{
std::cout << "\n-- initializer_list --" << std::endl;
// creating memory to vector using initializer list
std::vector<std::string> arrayOfVector{ "one", "two", "final" };
std::vector<std::string> arrayOfVector2{ "one2", "two2", "final2" };
std::vector<std::string> arrayOfVector3{ "one3", "two3", "final3" };
struct localFucntion
{
// function which take argument as initializer_list
void localVectorFun(std::initializer_list<std::vector<std::string>> val)
{
// vector base range loop
for (const auto &value : val) {
// string base range loop
for (const auto &inter : value) {
std::cout << inter << ", ";
}
std::cout << std::endl;
}
}
};
localFucntion lf;
lf.localVectorFun({ arrayOfVector, arrayOfVector2, arrayOfVector3 });
}
// preventing narrowing
void _9()
{
std::cout << "\n -- Preventing narrowing --" << std::endl;
const int iValue{ 2 };
//const int iValue2{ 3.4 }; // where else I can not have flote in int. It show error
std::cout << "const int iValue{ 2 } => " << iValue << std::endl;
}
// delegate constructor
void _10()
{
std::cout << "\n -- delegate constructor --" << std::endl;
class LocalClass
{
public:
LocalClass() : LocalClass{ 23 } {} // constructor to call parameterized constructor
LocalClass(int iX) : m_iX(iX)
{
std::cout << "called parameterized constructor " << std::endl;
}
auto getIX() const
{
return m_iX;
}
private:
int m_iX;
};
LocalClass lc;
std::cout << "m_iX : " << lc.getIX() << std::endl;
}
// in-class initializer
void _11()
{
std::cout << "\n -- In-class initializer --" << std::endl;
class LocalClass
{
public:
int iX{ 10 }; // in-class initializer
float fX{ 1.0 };
char cX{ 'A' };
std::string xX{ "HelloWorld" };
};
LocalClass lc;
std::cout << typeid(lc.iX).name() << " " << lc.iX << std::endl;
std::cout << typeid(lc.fX).name() << " " << lc.fX << std::endl;
std::cout << typeid(lc.cX).name() << " " << lc.cX << std::endl;
std::cout << "std::string" << " " << lc.xX << std::endl;
}
// inhereted constructor
void _12()
{
std::cout << "\n -- inhereted constructor -- " << std::endl;
struct BaseClass
{
BaseClass(int val) : iBX(val) {}
int iBX{ 0 };
};
struct DerivedClass : public BaseClass
{
using BaseClass::BaseClass; // inhereted BaseClass
};
// using baseclass constructor you can init derived class
DerivedClass dc(129);
std::cout << typeid(dc).name() << " " << dc.iBX << std::endl;
}
// static_assert
void _13()
{
std::cout << "\n -- static_assert -- " << std::endl;
// uncomment to check this function
static_assert(1 == true, "yes"); // successed assert will be elemenited from compling
//static_assert(0 == true, "on no");
}
// long long
void _14()
{
std::cout << "\n -- long long --" << std::endl;
long long x = 9223372036854775807LL;
std::cout << x << std::endl;
}
// nullptr
void _15()
{
std::cout << "\n -- nullptr --" << std::endl;
int *pIX = nullptr;
float *pCX = nullptr;
std::cout << typeid(pIX).name() << " " << pIX << std::endl;
std::cout << typeid(pCX).name() << " " << pCX << std::endl;
}
// suffix return type syntax
void _16()
{
std::cout << "\n -- suffix return type syntax --" << std::endl;
// local class
auto getRanVal = [](auto iX, auto iY) -> decltype(iX) {
return iX*iY;
};
std::cout << "Return int: " << getRanVal(10, 10) << std::endl;
std::cout << "Return double: " << getRanVal(12.4, 12.43) << std::endl;
}
// template alise
void _17()
{
std::cout << "\n -- template alise --" << std::endl;
//template<typename T> using ptr = T*;
// the name 'ptr<T>' is now an alias for pointer to T
//ptr<int> ptr_int; // not present in VS2015, can be present in 2017RC
std::cout << "not present in VS2015, can be present in 2017RC" << std::endl;
}
// varadic template
namespace VaradicTemplate {
class LocalClass
{
public:
template<typename T>
T getFun(T v)
{
return v;
}
template<typename T, typename... Args> // template has Args of template pack, if this in function then its called funciton pack
T getFun(T v, Args... args)
{
return v + getFun(args...);
}
};
}
void _18()
{
std::cout << "\n -- varadic template --" << std::endl;
VaradicTemplate::LocalClass lc;
long long sum = lc.getFun(1, 3, 4, 6, 4, 32, 65, 23, 635, 34, 654, 234, 45456, 34);
std::cout << "Varadic Template: " << sum << std::endl;
}
// Rvalue referance
void _19()
{
std::cout << "\n -- RValue referance --" << std::endl;
class LocalClass
{
public:
LocalClass() = default;
~LocalClass() = default;
LocalClass(const LocalClass&) = default;
LocalClass& operator=(const LocalClass&) = default;
LocalClass(const LocalClass&&) = delete; // rvalue referance
LocalClass& operator=(const LocalClass&&) = delete; // rvalue referance
};
std::cout << "RValue referance &&" << std::endl;
}
// unions
void _20()
{
std::cout << "\n -- union --" << std::endl;
union LocalUnion {
int iX;
std::string str; // now we can have string, seriouse invariant
~LocalUnion() {}
};
LocalUnion lu = { 90 };
std::cout << "union cpp11 : " << lu.iX << std::endl;
std::cout << "union sizeof : " << sizeof(lu) << std::endl;
}
// raw string iterator
void _21()
{
std::cout << "\n -- raw string iterator --" << std::endl;
std::string raw_str = R"("Quoted str "\/" ")";
std::cout << "Raw str R: " << raw_str << std::endl;
}
// user-define literals
namespace UserDefineLiterals {
constexpr long double operator"" _x2(long double val)
{
return val * 2;
}
}
void _22()
{
std::cout << "\n -- user-define literals --" << std::endl;
using namespace UserDefineLiterals;
auto result = 100.0_x2; // using user define literals
std::cout << "result 100.0_x2: " << result << std::endl;
}
// attributes
void _23()
{
std::cout << "\n -- attributes --" << std::endl;
class LocalClass
{
public:
void fun[[noreturn]]()
{
std::cout << "fun() has [[noreturn]] attribute" << std::endl;
}
void fun2[[carries_dependency]]()
{
std::cout << "fun2() has [[carries_dependency]] attribute" << std::endl;
}
};
LocalClass lc;
lc.fun();
lc.fun2();
}
// lambdas
void _24()
{
std::cout << "\n -- lamda --" << std::endl;
auto compare = [](const auto a, const auto b) -> bool {
return a > b;
};
std::cout << "(10 > 8) : " << std::boolalpha << compare(10, 8) << std::endl;
std::cout << "(900.0 > 900.5) : " << std::boolalpha << compare(900.0, 900.5) << std::endl;
std::cout << "(str > strr)" << std::boolalpha << compare("str", "strr") << std::endl;
}
// local type as template argument
void _25()
{
std::cout << "\n -- local type as template argument --" << std::endl;
struct LocalStruct // local structure
{
int iX{ 0 };
};
LocalStruct a1, a2, a3, a4, a5;
std::vector<LocalStruct> arrayOfVector{ a1, a2, a3, a4, a5 }; // using local struct in template argument
for (const auto &T : arrayOfVector) {
std::cout << T.iX << ",";
}
}
// noexcept
void _26()
{
std::cout << "\n -- noexcept --" << std::endl;
class LocalStreet
{
std::vector<int> vec{ 1,2,3,4,5,6 };
public:
void fun[[noreturn]]() noexcept
{
std::cout << "This function will not throw as noexcept made" << std::endl;
}
void fun2() noexcept(true)
{
std::cout << "This function will not throw as noexcept(true) made" << std::endl;
}
};
LocalStreet ls;
ls.fun();
ls.fun2();
}
// alignment
void _27()
{
std::cout << "\n -- alignment --" << std::endl;
alignas(8) unsigned char hugeCharBuffer[1024];
unsigned char normalCharBuffer[1024];
std::cout << "normalCharBuffer size: " << sizeof(normalCharBuffer) << std::endl;
std::cout << "hugeCharBuffer size: " << sizeof(hugeCharBuffer) << std::endl;
std::cout << "int size: " << std::alignment_of<int>() << std::endl;
std::cout << "double size: " << std::alignment_of<double>() << std::endl;
}
// override
class LocalStreet_28
{
public:
virtual void printLocalStreet_28(std::string str) = 0;
};
class DrivedLocalStreet_28 : public LocalStreet_28
{
public:
void printLocalStreet_28(std::string str) override
{
std::cout << "printLocalStreet_28: " << str << std::endl;
}
// void printLocalStreet_28(char* str) override { } // will cause error
};
void _28()
{
std::cout << "\n -- override --" << std::endl;
std::unique_ptr<LocalStreet_28> pLocalStreetObj(new DrivedLocalStreet_28);
pLocalStreetObj->printLocalStreet_28("called from _28() function.");
}
// final
class LocalStreet_29
{
public:
virtual void setPrint() final {
std::cout << "setPrint as final" << std::endl;
}
virtual int getPrint() const
{
return 10;
}
};
class DeriveLocalStreet_29 : LocalStreet_29
{
public:
int getPrint() const
{
return 100;
}
};
void _29()
{
std::cout << "\n -- final --" << std::endl;
std::unique_ptr<LocalStreet_29> pLocalStreetObj(new LocalStreet_29);
std::cout << "final: " << pLocalStreetObj->getPrint() << std::endl;
}
// any_of, all_of, none_of
void _30()
{
std::cout << "\n -- any_of, all_of, none_of -- " << std::endl;
std::array<int, 10> listOfArray = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
bool bIsAllOfEven = std::all_of(listOfArray.begin(), listOfArray.end(), [](int iValue) {
return iValue % 2 == 0;
});
bool bIsAnyOfOdd = std::any_of(listOfArray.begin(), listOfArray.end(), [](int iValue) {
return iValue % 2 != 0;
});
bool bIsNoneOfOdd = std::none_of(listOfArray.begin(), listOfArray.end(), [](int iValue) {
return iValue % 2 != 0;
});
std::cout << "all_of : " << bIsAllOfEven << std::endl;
std::cout << "any_of : " << bIsAnyOfOdd << std::endl;
std::cout << "none_of : " << bIsNoneOfOdd << std::endl;
}
// non-modifing sequence operator
void _31()
{
std::cout << "\n -- non-modifing sequence operator --" << std::endl;
std::array<int, 10> listOfArray = { 2, 4, 6, 8, 1, 12, 14, 16, 18, 20 };
// for_each
std::for_each(listOfArray.begin(), listOfArray.end(), [](int iValue) {
std::cout << "for_each: " << iValue << std::endl;
});
// find_if_not
std::array<int, 10>::iterator it = std::find_if_not(listOfArray.begin(), listOfArray.end(), [](int iValue) {
return iValue % 2 == 0;
});
std::cout << "find_if_not: " << *it << std::endl;
// find_if
std::array<int, 10>::iterator ij = std::find_if(listOfArray.begin(), listOfArray.end(), [](int iValue) {
return iValue % 2 == 0;
});
std::cout << "find_if: " << *ij << std::endl;
// count
auto countVal = std::count(listOfArray.begin(), listOfArray.end(), 6);
std::cout << "Count '6': " << countVal << std::endl;
// count_if
auto countIfVal = std::count_if(listOfArray.begin(), listOfArray.end(), [](int iVal) {
return iVal % 2 == 0;
});
std::cout << "Count 'count_if': " << countIfVal << std::endl;
// mismatch
}
int main()
{
// cpp version
_1();
// auto
_2();
// Rangebase loop
_3();
// default and delete
_4();
// enum
_5();
// constexpr
_6();
// decltype
_7();
// initializer_list
_8();
// Preventing narrowing
_9();
// delegate constructor
_10();
// in-class initializes
_11();
// inhereted constructor
_12();
// static_assert
_13();
// long long
_14();
// nullptr
_15();
// suffix return type syntax
_16();
// template alise
_17();
// varadic template
_18();
// RValue referance
_19();
// union
_20();
// raw string iterator
_21();
// user define-literals
_22();
// attribute
_23();
// lambdas
_24();
// local type template argument
_25();
// noexcept
_26();
// alignment
_27();
// override
_28();
// final
_29();
// any_of, all_of, none_of
_30();
// non - modifing sequence operator
_31();
auto val = 0;
std::cin >> val;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment