Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Structured Member Value Access Using Structure Member Pointer and Vari...
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 #include <iostream> #include <typeinfo> #include <string> using namespace std; struct TRC { int data; std::string str; }; struct XRC { int v; TRC trc; }; struct CRC { int k; float g; XRC xrc; }; struct EX_STRUCT { int a; float b; char ch; CRC crc; }; template<typename T, typename Visitable> T& GetValueRef(Visitable& obj, T Visitable::* member) { return obj.*member; } template <typename T> T* GetValueAddress(T& obj) { return &obj; } template<typename ObjectReference, typename MemberPointer, typename... Args> void* GetValueAddress(ObjectReference& objRef, MemberPointer memberPtr, Args... args) { return GetValueAddress(GetValueRef(objRef, memberPtr), args...); }; template<typename T, typename... Args> T GetValue(Args... args) { void * valPtr = GetValueAddress(args...); return *reinterpret_cast<T*>(valPtr); } EX_STRUCT myStruct; int main() { myStruct.a = 200; myStruct.b = 3.14f; myStruct.crc.k = 450; myStruct.crc.xrc.v = 111; myStruct.crc.xrc.trc.data = 555; myStruct.crc.xrc.trc.str = "Hello World"; std::cout << "EX_STRUCT::a value: " << GetValue<int>(myStruct, &EX_STRUCT::a) << endl; std::cout << "EX_STRUCT::b value: " << GetValue<float>(myStruct, &EX_STRUCT::b) << endl; std::cout << "EX_STRUCT::CRC::k value: " << GetValue<int>(myStruct, &EX_STRUCT::crc, &CRC::k) << endl; std::cout << "EX_STRUCT::CRC::XRC::v value: " << GetValue<int>(myStruct, &EX_STRUCT::crc, &CRC::xrc, &XRC::v) << endl; std::cout << "EX_STRUCT::CRC::XRC::TRC::data value: "<<GetValue<int>(myStruct, &EX_STRUCT::crc, &CRC::xrc, &XRC::trc, &TRC::data) << endl; std::cout << "EX_STRUCT::CRC::XRC::TRC::str value: " << GetValue<std::string>(myStruct, &EX_STRUCT::crc, &CRC::xrc, &XRC::trc, &TRC::str) << endl; return 0; }
cl.exe
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1,76 sec, absolute running time: 0,22 sec, absolute service time: 1,99 sec
edit mode
|
history
|
discussion