Variable number of arguments in C++?

后端 未结 17 1787
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:13

How can I write a function that accepts a variable number of arguments? Is this possible, how?

17条回答
  •  悲哀的现实
    2020-11-21 23:29

    It is possible now...using boost any and templates In this case, arguments type can be mixed

    #include 
    #include 
    
    #include 
    using boost::any_cast;
    
    template  
    void Alert(T var1,Types... var2) 
    { 
    
        std::vector a(  {var1,var2...});
    
        for (int i = 0; i < a.size();i++)
        {
    
        if (a[i].type() == typeid(int))
        {
            std::cout << "int "  << boost::any_cast (a[i]) << std::endl;
        }
        if (a[i].type() == typeid(double))
        {
            std::cout << "double "  << boost::any_cast (a[i]) << std::endl;
        }
        if (a[i].type() == typeid(const char*))
        {
            std::cout << "char* " << boost::any_cast (a[i]) <

提交回复
热议问题