Sprout で指定した値で配列の初期化を行う

sprout::fill を使用します。

[ソース]

#include <sprout/array.hpp>
#include <sprout/algorithm/fill.hpp>
#include <sprout/algorithm/fill_n.hpp>
#include <sprout/pit.hpp>

int
main(){
    // 今回は、初期化時に fill して欲しいので
    // 処理結果の型を sprout::pit でラップして引数に渡す
    static constexpr auto result = sprout::fill(sprout::pit<sprout::array<int, 4>>{}, 42);
    static_assert(result == sprout::make_array<int>(42, 42, 42, 42), "");

    // n まで埋める場合
    static constexpr auto result2 = sprout::fill_n(result, 2, -1);
    static_assert(result2 == sprout::make_array<int>(-1, -1, 42, 42), "");

    return 0;
}

[コンパイラ]

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