www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - __traits isCopyable vs isPOD

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
For which types `T` does

```d
__traits(isCopyable, T)
```

differ from

```d
__traits(isPOD, T)
```

?
Nov 28 2022
next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 28 November 2022 at 20:58:43 UTC, Per Nordlöw wrote:
 For which types `T` does

 ```d
 __traits(isCopyable, T)
 ```

 differ from

 ```d
 __traits(isPOD, T)
 ```

 ?
I'm asking because I have code like ```d static if (__traits(isCopyable, Element)) insertAt(element, index); else insertAt(move(element), index); ``` and I wonder if one in those cases should be using `isCopyable` or `isPOD`.
Nov 28 2022
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 28 November 2022 at 20:58:43 UTC, Per Nordlöw wrote:
 For which types `T` does

 ```d
 __traits(isCopyable, T)
 ```

 differ from

 ```d
 __traits(isPOD, T)
 ```

 ?
Lots of types. For example, types with copy constructors or destructors are not POD but may still be copyable. This should be obvious if you read the definition of POD linked from the language spec: https://dlang.org/glossary.html#pod
Nov 28 2022
parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 28 November 2022 at 22:59:13 UTC, Paul Backus wrote:
 Lots of types. For example, types with copy constructors or 
 destructors are not POD but may still be copyable.

 This should be obvious if you read the definition of POD linked 
 from the language spec: https://dlang.org/glossary.html#pod
I guess I knew that, sorry for the dumb question - the real question I had is whether one should use `isPOD` instead of `isCopyable` in cases ```d static if (__traits(isCopyable, Element)) insertAt(element, index); else insertAt(move(element), index); ``` because that avoids any potential call to the copy-constructor and destructor of `Element`. Afaict, one should.
Nov 28 2022
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 28 November 2022 at 23:11:37 UTC, Per Nordlöw wrote:
 the real question I had is whether one should use `isPOD` 
 instead of `isCopyable` in cases

 ```d
             static if (__traits(isCopyable, Element))
                 insertAt(element, index);
             else
                 insertAt(move(element), index);
 ```

 because that avoids any potential call to the copy-constructor 
 and destructor of `Element`. Afaict, one should.
If your goal is to avoid calling the copy constructor (and, I assume, to avoid unnecessary instantiations of `move`), then yeah, `isPOD` is the one you want.
Nov 28 2022
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 29 November 2022 at 00:50:54 UTC, Paul Backus wrote:
 If your goal is to avoid calling the copy constructor (and, I 
 assume, to avoid unnecessary instantiations of `move`), then 
 yeah, `isPOD` is the one you want.
Thanks.
Nov 29 2022