c++ 变长模板

来源:互联网 发布:金蝶kis软件视频教程 编辑:程序博客网 时间:2024/06/13 01:58
template<typename T>
void f(const T& t)
{
    cout << t << endl;
}

template<typename T, typename... Arg>
void f(const T& t,   const Arg&... arg)
{
    cout << t << endl;
    f(arg...);
    
}


int main()
{
    f(10, 20, 30);

    system("pause");

    return 0;

}



//////////////////

void print_split(std::ostream&)
{

}

template<class T,class... Types>
void print_split(std::ostream& stream, T&& head, Types&&... rest)
{
    stream << head << ' ';
    print(stream, std::forward<Types>(rest)...);
}

template<class... Types>
void print(std::ostream& stream, Types&&...args)
{
    print_split(stream, std::forward<Types>(args)...);
}


int main()
{
    print(std::cout, 42, 'x', "hello", 3.14159, 0, '\n');

    system("pause");
    return 0;

}


////////////////////////////////////////////////////

template<long ...nums> struct Multiply;

template<long first, long ... last>
struct Multiply<first, last...>
{
    static const long val = first * Multiply<last...>::val;
};

template<>
struct Multiply<>
{
    static const long val = 1;
};


int main()
{
    cout<< Multiply<1, 2, 3, 4, 5>::val << std::endl;

    std::tuple<int, double, std::string> m = std::make_tuple(1, 22.2, "good");


    system("pause");

    return 0;
}


0 0
原创粉丝点击