C++11 variable number of arguments, same specific type

前端 未结 6 1839
难免孤独
难免孤独 2020-12-05 01:55

Question is simple, how would I implement a function taking a variable number of arguments (alike the variadic template), however where all arguments have the same type, say

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 02:38

    @Skeen How about this?

    template 
    void func_1(std::initializer_list&& a) {
        // do something
    } 
    
    template 
    void func(T&&... a) {
        func_1({std::forward(a)...});
    } 
    
    int main() {
        func(1, 2, 3);
        // func(1, 2, 3, 4.0); // OK doesn't compile
    }
    

提交回复
热议问题