https://github.com/facontidavide/safecppany
Extension of std::any with small object optmization and safe numeric conversion.
https://github.com/facontidavide/safecppany
any cpp safe
Last synced: about 1 year ago
JSON representation
Extension of std::any with small object optmization and safe numeric conversion.
- Host: GitHub
- URL: https://github.com/facontidavide/safecppany
- Owner: facontidavide
- License: bsd-3-clause
- Created: 2015-10-27T08:27:53.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-12-12T10:29:55.000Z (over 7 years ago)
- Last Synced: 2024-10-11T14:11:32.703Z (over 1 year ago)
- Topics: any, cpp, safe
- Language: C++
- Homepage:
- Size: 195 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# std::any with small object optimization and safe numerical conversion
The purpose of this tiny header-only library is to provide a type called SafeAny::Any which can hold any
type, just like std::any or boost::any.
But it has few importants features:
- It has small object optimization for objects smaller than two words (16 bytes on a 64bits platform), thanks to this 3rdparty implementation [thelink2012/any](https://github.com/thelink2012/any).
- it is less pedantic than usual when an arithmetic value (integer, floating point, enum, etc.) is casted into another one. The library will check for narrowing, signedness conversions and numerical cancellation.
## Example
```c++
using SafeAny::Any;
{
Any val( int(-1000) );
int int_val = val.cast(); //OK
double real_val = val.cast(); //OK
double long_val = val.cast(); //OK
int8_t small_val = val.cast(); // throws exception
uint16_t unsigned_val = val.cast(); // throws exception
}
{
Any val( float(3.1) );
float f_val = val.cast(); //OK
double d_val = val.cast(); //OK
int i_val = val.cast(); // throws exception.
}
```
## Implementation
SafeAny extends std::any with few ideas from [Poco::DynamicAny](http://pocoproject.org/docs-1.4.6/Poco.DynamicAny.html).
The main drawback of Poco::DynamicAny is that it requires heap allocation, something that is slow and not very cache-friendly.