|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r70636 - sandbox/local/libs/local/example
From: lorcaminiti_at_[hidden]
Date: 2011-03-27 11:05:34
Author: lcaminiti
Date: 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
New Revision: 70636
URL: http://svn.boost.org/trac/boost/changeset/70636
Log:
Added compiling examples for Alternatives docs.
Added:
sandbox/local/libs/local/example/add_boost_lambda.cpp (contents, props changed)
sandbox/local/libs/local/example/add_boost_local.cpp (contents, props changed)
sandbox/local/libs/local/example/add_boost_local_va.cpp (contents, props changed)
sandbox/local/libs/local/example/add_boost_phoenix.cpp (contents, props changed)
sandbox/local/libs/local/example/add_cpp0x_lambda.cpp (contents, props changed)
sandbox/local/libs/local/example/add_local_class.cpp (contents, props changed)
Added: sandbox/local/libs/local/example/add_boost_lambda.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_boost_lambda.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,33 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+//[ add_boost_lambda_cpp
+#include <boost/lambda/lambda.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ // Passed as template parameter and also defined at expression level.
+ std::for_each(v.begin(), v.end(), (
+ // Unfortunately, cannot make `factor` constant.
+ // Unfortunately, body cannot use normal C++ syntax.
+ sum += factor * boost::lambda::_1,
+ boost::lambda::var(std::cout) << "Summed: " <<
+ boost::lambda::var(sum) << "\n"
+ ));
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
Added: sandbox/local/libs/local/example/add_boost_local.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_boost_local.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,37 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+//[ add_boost_local_cpp
+#include <boost/local/function.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ // Unfortunately, cannot be defined at expression level.
+ void BOOST_LOCAL_FUNCTION_PARAMS( (double num)
+ // Variable `factor` bound as constant.
+ (bind& sum) (const bind& factor) ) {
+ // Body uses normal C++ syntax.
+ sum += factor * num;
+ std::cout << "Summed: " << sum << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ // Local function `add` passed as template parameter.
+ std::for_each(v.begin(), v.end(), add);
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
+#endif
+
Added: sandbox/local/libs/local/example/add_boost_local_va.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_boost_local_va.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,46 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+#include <boost/config.hpp>
+#if defined(BOOST_NO_VARIADIC_MACROS) || defined(BOOST_LOCAL_CONFIG_COMPLIANT)
+#include <iostream>
+int main() {
+ std::cerr << "Error: This program requires variadic macros" << std::endl;
+ return 0;
+}
+#else
+
+//[ add_boost_local_va_cpp
+#include <boost/local/function.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ // Unfortunately, cannot be defined at expression level.
+ void BOOST_LOCAL_FUNCTION_PARAMS(double num,
+ // Variable `factor` bound as constant.
+ bind& sum, const bind& factor) {
+ // Body uses normal C++ syntax.
+ sum += factor * num;
+ std::cout << "Summed: " << sum << std::endl;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+ // Local function `add` passed as template parameter.
+ std::for_each(v.begin(), v.end(), add);
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
+#endif
+
Added: sandbox/local/libs/local/example/add_boost_phoenix.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_boost_phoenix.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,33 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+//[ add_boost_phoenix_cpp
+#include <boost/spirit/include/phoenix.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ // Passed as template parameter and also defined at expression level.
+ std::for_each(v.begin(), v.end(), (
+ // Unfortunately, cannot make `factor` constant.
+ // Unfortunately, body cannot use normal C++ syntax.
+ boost::phoenix::ref(sum) += factor * boost::phoenix::arg_names::_1,
+ std::cout << boost::phoenix::val("Summed: ") <<
+ boost::phoenix::ref(sum) << "\n"
+ ));
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
Added: sandbox/local/libs/local/example/add_cpp0x_lambda.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_cpp0x_lambda.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,42 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+#include <boost/config.hpp>
+#ifdef BOOST_NO_LAMBDAS
+#include <iostream>
+int main() {
+ std::cerr << "Error: This program requires C++0x lambdas" << std::endl;
+ return 0;
+}
+#else
+
+//[ add_cpp0x_lambda_cpp
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ // Passed as template parameter and also defined at expression level.
+ std::for_each(v.begin(), v.end(), [&sum, &factor](double num) {
+ // Unfortunately, cannot make `factor` constant.
+ // Body uses normal C++ syntax.
+ sum += factor * num;
+ std::cout << "Summed: " << sum << std::endl;
+ });
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
+#endif
+
Added: sandbox/local/libs/local/example/add_local_class.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_local_class.cpp 2011-03-27 11:05:33 EDT (Sun, 27 Mar 2011)
@@ -0,0 +1,39 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+//[ add_local_class_cpp
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ struct local {
+ static void add(double num,
+ // Unfortunately, cannot bind so repeat local variable types.
+ double& sum, const int& factor) {
+ // Body uses normal C++ syntax.
+ sum += factor * num;
+ std::cout << "Summed: " << sum << std::endl;
+ }
+ };
+
+ // Unfortunately, cannot pass as template parameter to `std::for_each`.
+ for (size_t i = 0; i < v.size(); ++i) {
+ // Unfortunately, explicitly pass variables `sum` and `factor`.
+ local::add(v[i], sum, factor);
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk