Variadic Templates から range-based for へ

とか書いてみました。

[ソース]

#include <iostream>
#include <type_traits>

template<typename ...Args>
using common_type = typename std::common_type<Args...>::type;


template<
    typename ...Args,
    typename CommonType = common_type<Args...>
>
void
disp(Args&&... args){
    for(auto&& n : { static_cast<CommonType>(args)...} ){
        std::cout << n << std::endl;
    }
}


#include <string>

int
main(){
    disp(42, 3.14f, 0.51);
    disp("mami", "an", std::string("homu"));

    return 0;
}

[出力]

42
3.14
0.51
mami
an
homu

Variadic Templates から initializer list へと変換して range-based for を使っているだけですね。
書いてみたものの使い道がよく分からない。

[コンパイラ]

  • g++ (GCC) 4.7.0 20111210 (experimental)