c++ 可變參數模板 實例

#include <iostream>
#include <typeinfo>
using namespace std;

template <typename ... T>
void Wrapper(T... t) {};

template<typename T>
const T& getbool(bool& rel, const T& data) {
    auto* typeInfo = &typeid(data);
    cout<<typeInfo->name() << ":" << data << endl;
    if(typeInfo == &typeid(bool)) {
        rel = rel && false;
    }
    rel = rel && true;
    return data;
}

template<typename T, typename ...Args>
bool getBool(const T& data, Args ... args) {
    bool succ = true;
    getbool(succ, data);
    Wrapper(getbool(succ, args)...);
    return succ;
}

int main()
{
    cout << getBool(1, "2", 100.0f) << endl;
    return 0;
}


#include <iostream>
#include <typeinfo>
using namespace std;

template <typename ... T>
void Wrapper(T... t) {};

template<typename T>
bool getBool(const T& data) {
    auto* typeInfo = &typeid(data);
    cout<<typeInfo->name() << ":" << data << endl;
    if(typeInfo == &typeid(bool)) {
        return false;
    }
    return true;
}

template<typename T, typename ...Args>
bool getBool(const T& data, Args ... args) {
    return getBool(data) && getBool(args...);
}

int main()
{
    cout << getBool(1, "2", 100.0f, false) << endl;
    return 0;
}
相關文章
相關標籤/搜索