C++20(templates and concepts)

what is template value type?

value_type of the template is the type of template type
for example if you have std::vector<int> the value type is int like the example below …

template<typename T>
typename std::decay<typename T::value_type>::type fun2(T t){
    return *t.begin();
}

what is the “requires” keyword?

in before c++ 20 if you want to restrict type

template<class T>
class A{
public:
    using value_type=T;
};
template<class T>
class B:public A<T>{};
template<typename T,typename =std::enable_if_t<std::is_base_of_v<A<typename T::value_type>,T>>,
        typename =std::enable_if<is_arithmetic_v<typename T::value_type>>>
class tempClass{};
//tempClass<B<int>> correct
//tempClass<B<string>> is not correct string is not arithmetic type

but in c++20

template<typename T>
requires std::is_base_of_v<A<typename T::value_type>,T> && is_arithmetic_v<typename T::value_type>
class tempClass{};

what is the “concept” keyword?

template<class T>
concept MM=std::is_base_of_v<A<typename T::value_type>,T> && is_arithmetic_v<typename T::value_type>;


template<MM T>
class tempClass{};

//or

template<class T> requires MM<T>
class tempClass{};

C++ 20 (general subjects)

span array

void pspan(span<T> s) {
    cout << format("number of elements: {}\n", s.size());
    cout << format("size of span: {}\n", s.size_bytes());
    for(auto e : s) cout << format("{} ", e);
    cout << "\n";
}
int main() {
    int carray[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    pspan<int>(carray);
}

structure binding

int nums[] { 1, 2, 3, 4, 5 };
auto [ a, b, c, d, e ] = nums;
cout << format("{} {} {} {} {}\n", a, b, c, d, e);

array<int,5> nums { 1, 2, 3, 4, 5 };
auto [ a, b, c, d, e ] = nums;
cout << format("{} {} {} {} {}\n", a, b, c, d, e);

tuple<int, double, string> nums{ 1, 2.7, "three" };
auto [ a, b, c ] = nums;
cout << format("{} {} {}\n", a, b, c);

Initialize variables within if and switch statements

template<typename T>
const char * f(const T a) {
    return typeid(T).name();
}
int main() {
    cout << format("T is {}\n", f(47));
    cout << format("T is {}\n", f(47L));
    cout << format("T is {}\n", f(47.0));
    cout << format("T is {}\n", f("47"));
    cout << format("T is {}\n", f("47"s));
}

Use template argument deduction for simplicity and clarity

template<typename T>
const char * f(const T a) {
    return typeid(T).name();
}
int main() {
    cout << format("T is {}\n", f(47));
    cout << format("T is {}\n", f(47L));
    cout << format("T is {}\n", f(47.0));
    cout << format("T is {}\n", f("47"));
    cout << format("T is {}\n", f("47"s));
}
T is int
T is long
T is double
T is char const *

Use if constexpr to simplify compile-time decisions

template<typename T>
auto value_of(const T v) {
    if constexpr (std::is_pointer_v<T>) {
        return *v;  // dereference the pointer
    } else {
        return v;   // return the value
    }
}
int main() {
    int x{47};
    int* y{&x};
    cout << format("value is {}\n", value_of(x));  // value
    cout << format("value is {}\n", value_of(y));  
                                                // pointer
    return 0;
}

build GCC and G++

1- clone GCC repository

git clone git://gcc.gnu.org/git/gcc.git

2- add select branch

git branch -a #to list all branches
git checkout branch_name#to select wanted branch in our case it gcc-12

3- in the source folder you will find ./contrib/download_prerequisites  
execute it to download dependencies

4- call configuration https://gcc.gnu.org/install/configure.html

% mkdir objdir
% cd objdir
% srcdir/configure [options] [target]
../gcc/configure --prefix=/home/malabdali/gcc-12 --enable-languages=c,c++ --disable-multilib

if it succeded makefiles it will be generated here (objdir)
not that build results will be saved here /home/malabdali/gcc-12

5- start call make

sudo make -j 8

6- call make install

sudo make install

after install you can find folder(bin,lib,include) inside /home/malabdali/gcc-12