samedi 27 juin 2015

Using typename in std::remove_reference::type

Here is my code:

#include <iostream>
#include <thread>
#include <vector>

template<typename B>
void checkInt(B&& b, std::false_type){
    std::cout<<"this is not int"<<std::endl;
}   


template<typename B>
void checkInt(B&& b, std::true_type){
    std::cout<<"this is int"<<std::endl;
}   



template<typename B>
void forwarder(B&& b){
    checkInt(std::forward<B>(b), 
     std::is_integral<typename std::remove_reference<B>::type >()
    );
}       


int main() {

    forwarder(1);
    return 0;
}

What it does is that, it distinguishes if the argument is int type or not. It works fine but i am confused about this specific line:

std::is_integral<typename std::remove_reference<B>::type >()

I tried researching about it and found out that the keyword typename is used to declare type parameter / avoid arithmetic confusions. I can't seem to find any proper examples that is related to my issue after that. Why is it necessary to use typename on std::remove_reference<B>::type ?

Bonus question : From void checkInt(B&& b, std::true_type), is std::true_type equivalent to true or false or it's the "bool" itself? like what does std::is_integral<typename std::remove_reference<B>::type >() returns?

Aucun commentaire:

Enregistrer un commentaire