C ++ 11: variadic function templates (function template with variable length parameters) (continued)

Source: Internet
Author: User
ArticleDirectory
    • Code 1
    • Code 2
Code 1
# Include <iostream> # include <string> # include <boost/Ref. HPP> # include <boost/fusion/container. HPP> # include <boost/fusion/algorithm. HPP> # include <boost/lambda/Lambda. HPP> using namespace STD; using namespace boost: fusion; using namespace boost: Lambda; Template <typename first, typename... rest> void output (first const & first, rest const &... rest) {cout <first; Auto V = make_vector (boost: CREF (rest )...); for_each (v, cout <constant (',') <_ 1); cout <Endl;} template <typename first, typename... rest> first sum (first, rest const &... rest) {Auto V = make_vector (boost: CREF (rest )...); for_each (v, VAR (first) + = _ 1); return first;} int main () {double D = 2.3; output (42, "hello", D, 'A'); output ("hello", D, 'A'); output (D, 'A'); output ('A '); output (sum (1), sum (1, 2), sum (string (), 'A', 'B'); Return 0;} // 42, hello, 2.3, A // hello, 2.3, A // 2.3, A // 1, 3, AB
Code 2
// Filename:. CPP # ifndef boost_pp_is_iterating # include <iostream> # include <string> # include <boost/Ref. HPP> # include <boost/fusion/container. HPP> # include <boost/fusion/algorithm. HPP> # include <boost/lambda/Lambda. HPP> using namespace STD; using namespace boost: fusion; using namespace boost: Lambda; # define boost_pp_iteration_params_1 (3, (1, 5, ". CPP ") # include boost_pp_iterate () int main () {double D = 2.3; output (42," hello ", D, 'A'); output (" hello ", d, 'A'); output (D, 'A'); output (sum (1), sum (1, 2 ), sum (string (), 'A', 'B'); Return 0;} // 42, hello, 2.3, A // hello, 2.3, A // 2.3, A // 1, 3, AB # else # define n boost_pp_iteration () # define macro_cref (z, N, T) boost: CREF (boost_pp_cat (t, n )) template <boost_pp_enum_params (n, typename t)> void output (random (n, T, const & T) {cout <t0; Auto V = make_vector (boost_pp_enum_shifted (n, macro_cref, t); // boost: Fusion: vector <partition (n, T, const & boost_pp_intercept)> V = make_vector (boost_pp_enum_shifted (n, macro_cref, t); for_each (v, cout <constant (',') <_ 1); cout <Endl;} template <boost_pp_enum_params (n, typename t)> t0 sum (T0 t0 boost_pp_comma_if (boost_pp_not_equal (n, 1) substring (n, T, const & T) {Auto V = make_vector (boost_pp_enum_shifted (n, macro_cref, t); // boost: Fusion: vector <counts (n, T, const & boost_pp_intercept)> V = make_vector (boost_pp_enum_shifted (n, macro_cref, t )); for_each (v, VAR (t0) + = _ 1); Return t0 ;}# undef n # endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.