Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as C++ by Julian ( 4 years ago )
// Helper.h
    #pragma once
    #include <string_view>
    
    class ConstVariableView
    {
    public:
    	ConstVariableView(void* pData) noexcept;
    
    	void* pData;
    	char padding[8] = {};
    };
    
    struct Temporary
    {
    	int a;
    	int b;
    	int c;
    	int d;
    };
    
    [[nodiscard]] Temporary getTemporary(void);
    
    void accessData(ConstVariableView variable, std::wstring_view strName);

// Helper.cpp

    #include "Helper.h"
    
    #include <iostream>
    
    ConstVariableView::ConstVariableView(void* pData) noexcept :
    	pData(pData) {}

Temporary getTemporary(void)
{
	return { 0, 1, 2, 3 };
}
    
    void accessData(ConstVariableView variable, std::wstring_view strName)
    {
    	std::cout << ((Temporary*)(variable.pData))->a;
    }

// main.cpp
    #include <cstdint>
    #include <type_traits>
    
    #include "ConstVariableView.h"
    
    template<typename Type>
    using ConstPassSemantic = Type;
    
    template<typename Type>
    using ConstAttributeSemantic = std::add_lvalue_reference_t<std::add_const_t<ConstPassSemantic<Type>>>;
    
    template<typename Type>
    [[nodiscard]] inline ConstVariableView createConstVariableView(ConstAttributeSemantic<Type> value)
    {
    	return { (void*)&value };
    }
    
    template<typename Type>
    void save(ConstPassSemantic<Type> value, std::wstring_view strName)
    {
    	const auto view = createConstVariableView<Type>(value);
    
    	accessData(view, strName);
    }
    
    int main()
    {
    	save<Temporary>(getTemporary(), L"Test");
    }

 

Revise this Paste

Parent: 120335
Your Name: Code Language: