File: default_converter.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (38 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[/
  Copyright (c) Vladimir Batov 2009-2022
  Distributed under the Boost Software License, Version 1.0.
  See copy at http://www.boost.org/LICENSE_1_0.txt.
]

[section Default Converter]

[import ../example/default_converter.cpp]

The explicit converter as in

 int i = boost::convert<int>("123", converter).value();
 
provides considerable flexibility, configurability and efficiency. However, in certain contexts that might be not that important or even counter-productive if, for example, an application relies on certain consistent behavior associated with one particular converter type and configuration. To accommodate such a scenario ['Boost.Convert] introduces the concept of the ['default converter] implemented as  `boost::cnv::by_default`. 

[important There is no default converter set by default.] 

Consequently, without additional configuration steps the following call will fail to compile: 

 int i = boost::convert<int>("123").value(); // No converter provided
 
However, after `boost::cnv::by_default` is defined simply as:

[default_converter_declaration_simple]

or potentially configured with additional formatting:

[default_converter_declaration_formatted]

the code compiles and deploys `boost::cnv::cstream` when `boost::convert()` is called without an explicitly supplied converter:

[default_converter_example1]

The trade-off for the convenience is the rigid converter configuration (which in certain contexts might be the desired behavior) and a potential performance impact. When a converter is not provided explicitly, the default converter is created, potentially configured, deployed and destroyed for every `boost::convert()` call. Consequently, if efficiency of this particular component is important, then the implementation of `boost::cnv::by_default` will need to take that into account and to make sure those operations are cheap.

[endsect]