Matthijs Kooijman
2008-Jun-06 07:27 UTC
[LLVMdev] Adding DenseMap::FindAndConstruct with a default value
> Assuming the default value is not a valid entry in your map (for instance, > if you're using pointers), you can do: > > Foo& entry = DenseMap[Key] > if (entry == DefaultValue) > entry = constructNewValue();The problem here is that the DefaultValue is undefined. However, Chris suggested that the default value, ValueT(), is not undefined but simply zero. However, on IRC someone was quite positive that it would become undefined. Anyone that knows for sure (preferably from the language standard, not only from experience :-) Gr. Matthijs -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: Digital signature URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080606/56837638/attachment.sig>
Eli Friedman
2008-Jun-06 08:39 UTC
[LLVMdev] Adding DenseMap::FindAndConstruct with a default value
On Fri, Jun 6, 2008 at 12:27 AM, Matthijs Kooijman <matthijs at stdin.nl> wrote:>> Assuming the default value is not a valid entry in your map (for instance, >> if you're using pointers), you can do: >> >> Foo& entry = DenseMap[Key] >> if (entry == DefaultValue) >> entry = constructNewValue(); > The problem here is that the DefaultValue is undefined. However, Chris > suggested that the default value, ValueT(), is not undefined but simply zero.This is correct; from the standard: "The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, which is value-initialized." (Section 5.2.3 [expr.type.conv] in the C++0x draft standard; I don't have the published standard on hand.) And value-initialization is defined to be zero-initialization for scalar types. (Section 8.5 [dcl.init] in the C++0x draft.)> However, on IRC someone was quite positive that it would become undefined.The reason this is kind of confusing is that in a lot of places, the standard says something along the lines of "if this object is a non-POD type, value-initialize it; otherwise, leave it uninitialized" (examples include automatic variables and class members). -Eli
Matthijs Kooijman
2008-Jun-06 09:26 UTC
[LLVMdev] Adding DenseMap::FindAndConstruct with a default value
Hi Eli, thanks for the clarification, this saves me from implementing extra code :-) Gr. Matthijs -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: Digital signature URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080606/20c1a0f0/attachment.sig>
Possibly Parallel Threads
- [LLVMdev] Adding DenseMap::FindAndConstruct with a default value
- [LLVMdev] Adding DenseMap::FindAndConstruct with a default value
- [LLVMdev] Adding DenseMap::FindAndConstruct with a default value
- [LLVMdev] compile error when using overloaded = operator of DenseMap
- [LLVMdev] compile error when using overloaded = operator of DenseMap