Can c++11 parameter packs be used outside templates?

后端 未结 4 1358
慢半拍i
慢半拍i 2020-12-24 06:44

I was wondering if I could have parameter packs consisting of a single, explicitly specified, type. For example, something like this:

#include 

        
4条回答
  •  滥情空心
    2020-12-24 07:03

    You might specify the type you want to show:

    #include 
    
    template
    void show_type() {}
    
    template
    void show_type(const T& x, Rest... rest)
    {
        std::cout << x << std::endl;
        show_type(rest...);
    }
    
    template
    void foo(int x, Args... args)
    {
        show_type(x, args...);
    }
    
    struct X { };
    std::ostream& operator<<(std::ostream& o, X)
    {
        o << "x";
        return o;
    }
    
    
    int main()
    {
        foo(1, 2, 3);
        foo(1, 2, 3.0); // Implicit conversion
        // or just
        show_type(1, 2, 3);
        foo(1, 2, X()); // Fails to compile
    }
    

提交回复
热议问题