Skip to content

Commit 6feeeb3

Browse files
gongminminvprus
authored andcommitted
Using type_index to avoid RTTIs in program_options. (fixes #10347)
1 parent 0efb385 commit 6feeeb3

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

include/boost/program_options/value_semantic.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include <boost/any.hpp>
1313
#include <boost/function/function1.hpp>
1414
#include <boost/lexical_cast.hpp>
15+
#include <boost/type_index.hpp>
1516

1617

1718
#include <string>
1819
#include <vector>
19-
#include <typeinfo>
2020
#include <limits>
2121

2222
namespace boost { namespace program_options {
@@ -163,6 +163,7 @@ namespace boost { namespace program_options {
163163
bool m_zero_tokens;
164164
};
165165

166+
#ifndef BOOST_NO_RTTI
166167
/** Base class for all option that have a fixed type, and are
167168
willing to announce this type to the outside world.
168169
Any 'value_semantics' for which you want to find out the
@@ -174,17 +175,20 @@ namespace boost { namespace program_options {
174175
public:
175176
// Returns the type of the value described by this
176177
// object.
177-
virtual const std::type_info& value_type() const = 0;
178+
virtual const boost::typeindex::type_info& value_type() const = 0;
178179
// Not really needed, since deletion from this
179180
// class is silly, but just in case.
180181
virtual ~typed_value_base() {}
181182
};
183+
#endif
182184

183185

184186
/** Class which handles value of a specific type. */
185187
template<class T, class charT = char>
186-
class typed_value : public value_semantic_codecvt_helper<charT>,
187-
public typed_value_base
188+
class typed_value : public value_semantic_codecvt_helper<charT>
189+
#ifndef BOOST_NO_RTTI
190+
, public typed_value_base
191+
#endif
188192
{
189193
public:
190194
/** Ctor. The 'store_to' parameter tells where to store
@@ -359,10 +363,12 @@ namespace boost { namespace program_options {
359363

360364
public: // typed_value_base overrides
361365

362-
const std::type_info& value_type() const
366+
#ifndef BOOST_NO_RTTI
367+
const boost::typeindex::type_info& value_type() const
363368
{
364-
return typeid(T);
369+
return boost::typeindex::type_id<T>().type_info();
365370
}
371+
#endif
366372

367373

368374
private:

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ test-suite program_options :
3333
[ po-test unrecognized_test.cpp ]
3434
[ po-test required_test.cpp : required_test.cfg ]
3535
[ po-test exception_txt_test.cpp ]
36+
[ run options_description_test.cpp : : : <rtti>off <define>BOOST_NO_RTTI <define>BOOST_NO_TYPEID : options_description_no_rtti_test ]
3637
;
3738

3839
exe test_convert : test_convert.cpp ;

test/options_description_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ void test_type()
2525
("bar", value<string>(), "")
2626
;
2727

28+
#ifndef BOOST_NO_RTTI
2829
const typed_value_base* b = dynamic_cast<const typed_value_base*>
2930
(desc.find("foo", false).semantic().get());
3031
BOOST_CHECK(b);
31-
BOOST_CHECK(b->value_type() == typeid(int));
32+
BOOST_CHECK(b->value_type() == boost::typeindex::type_id<int>());
3233

3334
const typed_value_base* b2 = dynamic_cast<const typed_value_base*>
3435
(desc.find("bar", false).semantic().get());
3536
BOOST_CHECK(b2);
36-
BOOST_CHECK(b2->value_type() == typeid(string));
37+
BOOST_CHECK(b2->value_type() == boost::typeindex::type_id<string>());
38+
#endif
3739
}
3840

3941
void test_approximation()

0 commit comments

Comments
 (0)