首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在静态成员中不使用类名获取C++类的大小

在静态成员中不使用类名获取C++类的大小
EN

Stack Overflow用户
提问于 2022-02-17 02:12:09
回答 1查看 79关注 0票数 1
代码语言:javascript
复制
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.
};

我意识到您不能在this成员中访问static

我基本上是在寻找一种方法来引用静态成员函数中当前类的类型,而不直接使用类名来引用。最终,这将与不需要知道当前类的名称的预处理器步骤一起使用。

我觉得应该有一些简单的方法来做到这一点,但我还没有找到它经过一堆搜索,同时希望找到一些神奇的关键字,如decltype,我已经忘记了。

编译器信息:

代码语言:javascript
复制
 ;  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
EN

回答 1

Stack Overflow用户

发布于 2022-02-17 02:33:25

实际上,你所要求的是不可能的。类的static成员不能引用类的任何特定实例,而*this引用特定的实例:本身。如果您确实希望获得它的大小,则必须删除static标识符和constexpr标识符,因为*this不被视为constexpr。此外,正如其他评论所述,您正试图以更复杂的方式实现sizeof(<instancename>)操作员所能做的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71151655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档