Comments (7)
Use emplace
BETTER_ENUM(encodings_e, int,
RED,
GREEN,
BLUE
)
std::unordered_map<std::string, encodings_e> map;
map.emplace(std::make_pair("rr", encodings_e::RED);
encodings_e foo = map["rr"];
std::cout << foo._to_string() << std::endl;
Edit:
@aantron come to think of it, I believe emplace
is the only way to avoid using a default-constructed temporary when inserting into standard containers, maybe you could add that info to documentation?
from better-enums.
It's been a long time, but it looks like there is an intermediate call to a default constructor somewhere during the insert.
I'm not immediately sure what the best solution to this is, but you should be able to work around it in at least one way by declaring the constructor public
as desribed here: https://aantron.github.io/better-enums/OptInFeatures.html#DefaultConstructors.
from better-enums.
@jaskij Thanks!
Let's see if there is any word from @biziosan on whether emplace
fixes the issue, and then I'll add it to the docs.
from better-enums.
@aantron since I'm dealing with Better Enums now anyway I've tested this, also using gcc 9.3.
TL;DR: operator[]
for std::map
and std::unordered_map
requires the value type to be default constructible.
The result is that std::unordered_map::operator[]
(and likely std::map::operator[]
too) won't work. The reason is that operator[]
looks up the key and inserts a default-constructed value if the key is not present in the map. Even if we don't use it that way the compiler can't instantiate the operator if the value type is not default constrible.
Also, consider the following notes from operator[]
description on cppreference.com:
In the published C++11 and C++14 standards, this function was specified to require mapped_type to be DefaultInsertable and key_type to be CopyInsertable or MoveInsertable into *this. This specification was defective and was fixed by LWG issue 2469, and the description above incorporates the resolution of that issue.
However, one implementation (libc++) is known to construct the key_type and mapped_type objects via two separate allocator construct() calls, as arguably required by the standards as published, rather than emplacing a value_type object.
operator[] is non-const because it inserts the key if it doesn't exist. If this behavior is undesirable or if the container is const, at() may be used.
All in all, insert via emplace()
and access via at()
, those are sure to work.
Sample code:
#include <iostream>
#include <map>
#include <better-enums/enum.h>
BETTER_ENUM(mbool, char, mfalse = 0, mtrue = 1)
int main() {
std::map<std::string, mbool> map;
map.emplace(std::make_pair("true", mbool::mfalse));
//const auto t = map["true"]; // compiler error
const auto t = map.at("true");
//std::cout << map["true"] << std::endl; // compiler error
std::cout << map.at("true") << std::endl;
std::cout << t << std::endl;
return 0;
}
from better-enums.
I haven't tried emplace()
yet, but with the default constructor enabled my simple code works. I'll test emplace()
right away as well.
from better-enums.
@biziosan I don't know if you read my last message, but regardless of emplace()
, without a public default constructor you can't use operator[]
with maps, so that might be a pain point.
from better-enums.
@jaskij Hi, yes, thank you! It matches with I have working. I used @aantron suggestion about public default constructors and enabled them (https://aantron.github.io/better-enums/OptInFeatures.html#DefaultConstructors). That worked right away!
I think I am satisfied with the answers. I can close this issue if you want.
from better-enums.
Related Issues (20)
- Static functions for enum types HOT 1
- Use in a packed struct HOT 1
- MSVC /permissive- leads to "expression did not evaluate to a constant" HOT 3
- Compiling example code: 5-map.cc fail HOT 2
- enums stops being enums HOT 1
- What is the tool to run better-enums in REPL mode in README.md? HOT 2
- Compile-time name trimming and binary size
- How to reflect to enum with type and value strings? HOT 4
- Need to have standard Debian packaging capability
- Cannot use better_enum from class HOT 1
- Using Better Enums in std::unordered_map as key leads to 'message : see reference to class template instantiation'
- Timeframe for new release / tag
- Provide an update to make_macros.py script to use python3
- Set up GitHub Actions
- C++17 interop: std::optional HOT 2
- Way to opt out of string conversion? HOT 2
- cannot add better_enum values to unordered_set HOT 3
- Member '_value' was not initialized in this constructor HOT 1
- Are there guarantees on size with better enums? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from better-enums.