Cameron Tacklind
Cameron Tacklind

Reputation: 7234

Get sizeof C++ class in static member without using the class's name

struct MyClass {
  unsigned a;
  // ...

  static constexpr unsigned size_this = sizeof(*this); // Doesn't work
  static constexpr unsigned size_type = sizeof(MyClass); // Works in gcc ...
  // but I need the name "MyClass" in source.
};

I realize that you cannot access this inside of a static members.

I'm basically looking for a way to reference the current class's type from a static member function without using the class's name directly. Ultimately this is to be used with a preprocessor step that wouldn't otherwise need to know the name of the current class.

I feel like there should be some simple way to do this but I haven't found it after a bunch of searching while hoping to find some magical keyword like decltype that I'd forgotten.


Compiler info:

 ;  GNU C++14 (AVR_8_bit_GNU_Toolchain_3.6.2_1778) version 5.4.0 (avr)
 ;  compiled by GNU C version 4.7.4, GMP version 5.0.2, MPFR version 3.0.0, MPC version 0.9
 ;  GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 ;  options passed:  -I src -I lufa -I AVR++ -I libCameron/src
 ;  -imultilib avr5
 ;  -iprefix c:\program files (x86)\microchip\avr8-gnu-toolchain-win32_x86\bin\../lib/gcc/avr/5.4.0/
 ;  -MMD .bld/main.cpp.d -MF .bld/.dep/main.cpp.S.d -MP -MQ .bld/main.cpp.S
 ;  -D__AVR_ATmega32U4__ -D__AVR_DEVICE_NAME__=atmega32u4
 ;  -D F_CPU=16000000ULL -D ARCH=ARCH_AVR8 -D F_USB=16000000ULL
 ;  -D USE_LUFA_CONFIG_HEADER -U AVR src/main.cpp -mn-flash=1 -mmcu=avr5
 ;  -auxbase-strip .bld/main.cpp.S -O3 -Wall -Wfatal-errors -std=gnu++14
 ;  -fshort-enums -funsigned-char -funsigned-bitfields -fno-strict-aliasing
 ;  -fpack-struct -ffunction-sections -fverbose-asm -mn-flash=1
 ;  -mno-skip-bug -fno-rtti -fno-enforce-eh-specs -fno-exceptions
 ;  options enabled:  -Wmisspelled-isr -faggressive-loop-optimizations
 ;  -falign-functions -falign-jumps -falign-labels -falign-loops
 ;  -fauto-inc-dec -fbranch-count-reg -fchkp-check-incomplete-type
 ;  -fchkp-check-read -fchkp-check-write -fchkp-instrument-calls
 ;  -fchkp-narrow-bounds -fchkp-optimize -fchkp-store-bounds
 ;  -fchkp-use-static-bounds -fchkp-use-static-const-bounds
 ;  -fchkp-use-wrappers -fcombine-stack-adjustments -fcommon -fcompare-elim
 ;  -fcprop-registers -fcrossjumping -fcse-follow-jumps -fdefer-pop
 ;  -fdevirtualize -fdevirtualize-speculatively -fdwarf2-cfi-asm
 ;  -fearly-inlining -feliminate-unused-debug-types
 ;  -fexpensive-optimizations -fforward-propagate -ffunction-cse
 ;  -ffunction-sections -fgcse -fgcse-after-reload -fgcse-lm -fgnu-runtime
 ;  -fgnu-unique -fguess-branch-probability -fhoist-adjacent-loads -fident
 ;  -fif-conversion -fif-conversion2 -findirect-inlining -finline
 ;  -finline-atomics -finline-functions -finline-functions-called-once
 ;  -finline-small-functions -fipa-cp -fipa-cp-alignment -fipa-cp-clone
 ;  -fipa-icf -fipa-icf-functions -fipa-icf-variables -fipa-profile
 ;  -fipa-pure-const -fipa-ra -fipa-reference -fipa-sra
 ;  -fira-hoist-pressure -fira-share-save-slots -fira-share-spill-slots
 ;  -fisolate-erroneous-paths-dereference -fivopts -fkeep-static-consts
 ;  -fleading-underscore -flifetime-dse -flra-remat -flto-odr-type-merging
 ;  -fmath-errno -fmerge-constants -fmerge-debug-strings
 ;  -fmove-loop-invariants -fomit-frame-pointer -foptimize-sibling-calls
 ;  -foptimize-strlen -fpack-struct -fpartial-inlining -fpeephole
 ;  -fpeephole2 -fpredictive-commoning -fprefetch-loop-arrays
 ;  -freg-struct-return -freorder-blocks -freorder-functions
 ;  -frerun-cse-after-loop -fsched-critical-path-heuristic
 ;  -fsched-dep-count-heuristic -fsched-group-heuristic -fsched-interblock
 ;  -fsched-last-insn-heuristic -fsched-rank-heuristic -fsched-spec
 ;  -fsched-spec-insn-heuristic -fsched-stalled-insns-dep -fschedule-fusion
 ;  -fsemantic-interposition -fshow-column -fshrink-wrap -fsigned-zeros
 ;  -fsplit-ivs-in-unroller -fsplit-wide-types -fssa-phiopt -fstdarg-opt
 ;  -fstrict-overflow -fstrict-volatile-bitfields -fsync-libcalls
 ;  -fthread-jumps -ftoplevel-reorder -ftrapping-math -ftree-bit-ccp
 ;  -ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-coalesce-vars
 ;  -ftree-copy-prop -ftree-copyrename -ftree-dce -ftree-dominator-opts
 ;  -ftree-dse -ftree-forwprop -ftree-fre -ftree-loop-distribute-patterns
 ;  -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon
 ;  -ftree-loop-optimize -ftree-loop-vectorize -ftree-parallelize-loops=
 ;  -ftree-partial-pre -ftree-phiprop -ftree-pre -ftree-pta -ftree-reassoc
 ;  -ftree-scev-cprop -ftree-sink -ftree-slp-vectorize -ftree-slsr
 ;  -ftree-sra -ftree-switch-conversion -ftree-tail-merge -ftree-ter
 ;  -ftree-vrp -funit-at-a-time -funswitch-loops -fverbose-asm
 ;  -fzero-initialized-in-bss

Upvotes: 1

Views: 432

Answers (1)

K.R.Park
K.R.Park

Reputation: 1129

Actually, what you are asking is not possible. static member of a class cannot refer to any specific instance of the class, and *this refer to a specific instance: itself. If you really wish to get its size, you have to remove static identifier, and constexpr identifier, as *this is not treated as constexpr. Furthermore, as other comment said, what you are trying to achieve is what sizeof(<instancename>) operator can do, in more complex way.

Upvotes: 1

Related Questions