From 0591b1d855e79f6287a308793ae8ae8aec3447b6 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Mon, 16 Sep 2019 02:01:05 -0400 Subject: [PATCH] Add BOOST_NVP convenience macro --- doc/nvp.qbk | 10 ++++++++++ include/boost/core/nvp.hpp | 2 ++ test/nvp_test.cpp | 12 +++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/nvp.qbk b/doc/nvp.qbk index 233b329..73a37e5 100644 --- a/doc/nvp.qbk +++ b/doc/nvp.qbk @@ -54,6 +54,8 @@ template const nvp make_nvp(const char* name, T& value) noexcept; } /* boost */ + +#define BOOST_NVP(object) ``['see below]`` ``` [section Constructors] @@ -86,6 +88,14 @@ noexcept;`] [endsect] +[section Macros] + +[variablelist +[[`#define BOOST_NVP(object) see below`] +[Expands to `boost::make_nvp(BOOST_STRINGIZE(object), object)`.]]] + +[endsect] + [endsect] [section Acknowledgments] diff --git a/include/boost/core/nvp.hpp b/include/boost/core/nvp.hpp index deab002..4fead97 100644 --- a/include/boost/core/nvp.hpp +++ b/include/boost/core/nvp.hpp @@ -46,4 +46,6 @@ make_nvp(const char* n, T& v) BOOST_NOEXCEPT } /* boost */ +#define BOOST_NVP(v) boost::make_nvp(BOOST_STRINGIZE(v), v) + #endif diff --git a/test/nvp_test.cpp b/test/nvp_test.cpp index 2e8316e..c292659 100644 --- a/test/nvp_test.cpp +++ b/test/nvp_test.cpp @@ -6,7 +6,7 @@ Distributed under the Boost Software License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt) */ #include -#include +#include void test() { @@ -28,9 +28,19 @@ void test_factory() BOOST_TEST_EQ(&p.value(), &v); } +void test_macro() +{ + int v = 1; + boost::nvp p = BOOST_NVP(v); + BOOST_TEST_CSTR_EQ(p.name(), "v"); + BOOST_TEST_EQ(p.value(), 1); + BOOST_TEST_EQ(&p.value(), &v); +} + int main() { test(); test_factory(); + test_macro(); return boost::report_errors(); }