Bug 16485 – Add trait for determining whether a member variable is static or not

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-10T22:32:57Z
Last change time
2018-01-05T13:27:29Z
Keywords
trivial
Assigned to
No Owner
Creator
Jonathan M Davis

Comments

Comment #0 by issues.dlang — 2016-09-10T22:32:57Z
We should probably have a trait in std.traits either for checking whether a member variable is static or for checking that it's not static. It's something that seems to have come up several times recently in D.Learn and stackoverflow. I don't know that this is the best implementation, since it was just quickly thrown together, but this would be one possible implementation: template isStaticMember(T, string memberName) if(__traits(hasMember, T, memberName)) { mixin("alias member = " ~ T.stringof ~ "." ~ memberName ~ ";"); enum isStaticMember = !__traits(compiles, member.offsetof); } class C { int foo; static int bar; } void main() { static assert(!isStaticMember!(C, "foo")); static assert(isStaticMember!(C, "bar")); } But the fact that it's the offsetof property that is the key is definitely non-obvious and not something that it's reasonable to expect that your average programmer is going to think of, so putting something like this in std.traits would definitely be beneficial.
Comment #1 by issues.dlang — 2016-10-03T19:02:16Z
Comment #2 by github-bugzilla — 2017-03-08T14:37:13Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/d79da1d3cac16043b3599a234875c20c9a9e7b0c Issue 16485 - Add trait for testing if a member is static. https://github.com/dlang/phobos/commit/eb92dc9a88cc8e3a787a6347bc7c58df62af2637 Merge pull request #5112 from sprinkle131313/is-static-member Issue 16485 - Add trait for testing if a member is static. merged-on-behalf-of: Jack Stouffer <[email protected]>
Comment #3 by github-bugzilla — 2017-03-22T12:22:28Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/d79da1d3cac16043b3599a234875c20c9a9e7b0c Issue 16485 - Add trait for testing if a member is static. https://github.com/dlang/phobos/commit/eb92dc9a88cc8e3a787a6347bc7c58df62af2637 Merge pull request #5112 from sprinkle131313/is-static-member
Comment #4 by github-bugzilla — 2017-08-07T12:26:17Z
Commits pushed to newCTFE at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/d79da1d3cac16043b3599a234875c20c9a9e7b0c Issue 16485 - Add trait for testing if a member is static. https://github.com/dlang/phobos/commit/eb92dc9a88cc8e3a787a6347bc7c58df62af2637 Merge pull request #5112 from sprinkle131313/is-static-member
Comment #5 by github-bugzilla — 2018-01-05T13:27:29Z
Commits pushed to dmd-cxx at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/d79da1d3cac16043b3599a234875c20c9a9e7b0c Issue 16485 - Add trait for testing if a member is static. https://github.com/dlang/phobos/commit/eb92dc9a88cc8e3a787a6347bc7c58df62af2637 Merge pull request #5112 from sprinkle131313/is-static-member