mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 11:43:29 +01:00
Updated toString docs with StringMaker
This commit is contained in:
parent
43470b2606
commit
605d870384
@ -1,7 +1,7 @@
|
|||||||
# String conversions
|
# String conversions
|
||||||
|
|
||||||
Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes).
|
Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes).
|
||||||
Most built-in or std types are supported out of the box but there are two ways that you can tell Catch how to convert your own types (or other, third-party types) into strings.
|
Most built-in or std types are supported out of the box but there are three ways that you can tell Catch how to convert your own types (or other, third-party types) into strings.
|
||||||
|
|
||||||
## operator << overload for std::ostream
|
## operator << overload for std::ostream
|
||||||
|
|
||||||
@ -41,6 +41,20 @@ namespace Catch {
|
|||||||
|
|
||||||
Again ```T``` is your type and ```convertMyTypeToString``` is where you'll write whatever code is necessary to make your type printable. Note that the function must be in the Catch namespace, which itself must be in the global namespace.
|
Again ```T``` is your type and ```convertMyTypeToString``` is where you'll write whatever code is necessary to make your type printable. Note that the function must be in the Catch namespace, which itself must be in the global namespace.
|
||||||
|
|
||||||
|
## Catch::StringMaker<T> specialisation
|
||||||
|
|
||||||
|
There are some cases where overloading toString does not work as expected. Specialising StringMaker<T> gives you more precise, and reliable, control - but at the cost of slightly more code and complexity:
|
||||||
|
|
||||||
|
```
|
||||||
|
namespace Catch {
|
||||||
|
template<> struct StringMaker<T> {
|
||||||
|
static std::string convert( T const& value ) {
|
||||||
|
return convertMyTypeToString( value );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[Home](Readme.md)
|
[Home](Readme.md)
|
||||||
|
Loading…
Reference in New Issue
Block a user