Example #1
0
	// And let's use our `forArgs` function to print
	// the indices:
	static void print()
	{
		forArgs
		(
			[](auto x){ std::cout << x << " "; },

			// We can expand the matched indices here:
			TIs...
		);
	}
Example #2
0
 constexpr decltype(auto) forTuple(
     TFunction&& mFunction, TTuple&& mTuple)
 {
     return tupleApply(
         [&mFunction](auto&&... xs)
         {
             forArgs(mFunction, ECS_FWD(xs)...);
         },
         ECS_FWD(mTuple));
 }
Example #3
0
void chain(TF&& mFn, TArgs&&... mArgs)
{
    forArgs(
        [mFn](auto& mCont)
        {
            for(auto& x : mCont) mFn(x);
        },

        std::forward<TArgs>(mArgs)...);
}
Example #4
0
auto make_vector(TArgs&&... mArgs)
{
    // First problem: what type should we return?

    // We need to deduce a type suitable for all the
    // passed arguments.

    // `std::common_type_t` comes into play here.

    // Note:
    //
    //    * C++14 added many versions of commonly used
    //      type traits functions ending with "_t", that
    //      do not require the user to write `typename`
    //      at the beginning and `::type` at the end.
    //
    //    * C++11:
    //      using IntPtr = typename std::add_pointer<int>::type;
    //
    //    * C++14:
    //      using IntPtr = std::add_pointer_t<int>;
    //

    // `std::common_type_t` determines the common type among all
    // passed types - that is the type all passed types can be
    // implicitly converted to.

    // Computing the correct return type is now easy:
    using VectorItem = std::common_type_t<TArgs...>;
    std::vector<VectorItem> result;

    // We also know how many items we're going to put into
    // the vector - we can actually reserve the memory beforehand
    // as a small optimization.
    result.reserve(sizeof...(TArgs));

    // Now we use `forArgs` to generate the code that emplaces
    // the items into our vector:
    forArgs(
        // Our lambda needs to capture the vector and use
        // an "universal reference" to correctly forward
        // the passed argument to `std::vector::emplace_back`.
        [&result](auto&& x)
        {
            // We do not know the type of `x` - but we can
            // retrieve it using `decltype(x)`.
            result.emplace_back(std::forward<decltype(x)>(x));
        },

        std::forward<TArgs>(mArgs)...);

    return result;
}
Example #5
0
int main()
{
    forArgs(
        [](const auto& x){ std::cout << x << std::endl; },
        "hello",
        1,
        2,
        3
    );

    return 0;
}
Example #6
0
auto make_vector(TArgs&&... mArgs)
{
    using VectorItem = std::common_type_t<TArgs...>;
    std::vector<VectorItem> result;

    result.reserve(sizeof...(TArgs));

    forArgs(
        [&result](auto&& x)
        {
            result.emplace_back(std::forward<decltype(x)>(x));
        },

        std::forward<TArgs>(mArgs)...);

    return result;
}