#Get Item From Struct at compile time

73 messages · Page 1 of 1 (latest)

subtle remnant
#

I have tried to look up on stackoverflow and other sources on how to do this but I have not find a slick way to do it using general compile time/metaprogramming techniques and the only thing I have found are things that are 70+ lines which seems a bit execissive for such a small thing, anybody got any ideas?

hallow crescentBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

stray wharf
#

Need to be more specific.

subtle remnant
#

So Im trying to create a variable at compile time that will be a specific type of variable from a struct. So I would probably use a template to require the name of the variable im trying to assign and the type just to make it more clear what variable im targetting. So I want to pretty much be able to extract a value from any struct

#

So it would work like auto struct_value = get_struct_value<variable_name, variable type>

#

@stray wharf More clear?

stray wharf
#

Kind of, but what you are looking for is probably a member pointer.

subtle remnant
#

Source ref so I can read?

subtle remnant
#

Yea well it will return a pointer from the struct

#

But the problem I have is how to make it in template

#

Because it needs to take in two template variables

stray wharf
#

Would need further elaboration then.

silent storm
#

;compile ```cpp
#include <iostream>

struct MyStruct {
int a{};
float b{};
double c{};
};

template <typename T, auto M>
auto get_struct_value(const T& obj) {
return obj.*M;
}

int main() {
MyStruct s{42, 42.f, 42.0};
auto val_a = get_struct_value<MyStruct, &MyStruct::a>(s);
std::cout << "a = " << val_a << "\n";
}

something like this?
glossy joltBOT
#
Program Output
a = 42
subtle remnant
#

template <typename T, auto M>
auto get_struct_value(const T& obj) {
return obj.*M;
}
This could work

silent storm
#

(can also be made compile time with lite dusting of constexpr)

subtle remnant
#

But it dosent check the variable

silent storm
subtle remnant
#

So you should specify the type inside the template aswell

#

So that the pointer returned has the right type

#

So its just for checking to make sure that the variable has the right type

#

Because right now it just returns an int type

#

But I want to be able to specify any type to be refrenced to

#

@silent storm

silent storm
# subtle remnant So you should specify the type inside the template aswell

You can add a type check to the template using requires if you need to, but the code would have to make some assumptions on what you would call the variable or soemthing otherwise it could look like this ```cpp
#include <iostream>
#include <utility>

struct MyStruct {
int a;
float b;
double c;
};

template <typename T, typename M_T, auto M>
constexpr M_T get_struct_value(const T& obj)
requires std::is_same_v<std::remove_cvref_t<decltype(std::declval<T>().*M)>, M_T>
{
return obj.*M;
}

int main() {
static constexpr MyStruct s{42, 3.14f, 2.71828};
// Make sure we get an int
static constexpr auto val_a = get_struct_value<MyStruct, int, &MyStruct::a>(s);
std::cout << "a = " << val_a << "\n";
return 0;
}

subtle remnant
#

You are a saint, this seems to work

#

Thank you

#

!solved

hallow crescentBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

stray wharf
#

Still not sure what that would be used for.

subtle remnant
#

Im going to for example send images through a network

#

So Ill pull info from the sockaddr_in struct

#

Its a slicker way to do it in my opinion

#

Hopefully you get what I mean @stray wharf

stray wharf
#

Unfortunately I still don't.

subtle remnant
#

sockaddr_in has values needed to communicate with the network

#

So I would for example pull the in_addr and socket

stray wharf
#

OK, then how would this template stuff help?

#

Also paste code as text.

subtle remnant
#

I just find it slick to refrence only the necessary things instead of parsing the whole struct

#

But I might make it a struct instead of a variable

#

But its a good way to filter through the necessary values

stray wharf
#

Parsing where?

subtle remnant
#

To a function or something

#

Im not sure what values need to be used yet and where

stray wharf
#

Say I have a variable sockaddr_in address = getAddress(); how would your thing be better than just address.sin_addr?

subtle remnant
#

I dont like how it looks

#

And I might change the sin_addr value later

#

So creating a custom variable to store it is better

#

Since you are just refrencing the value of address.sin_addr in your example/conclusion

#

So refrencing would only work if I have a connection on one socket

#

But Im thinking of establishing a seperate connection to send the files

#

I could just make a new struct for that

#

But I find this method better since some things I might take from the other struct if I for example want both of them to use UTP or whatever

stray wharf
#

Demonstrate with some code.

subtle remnant
#

Ill demonstrate with some code when I have it

#

Like I said I dont currently have the code written

#

Prioritized this instead

#

But let me see

stray wharf
#

Pseudo-code is fine, I just need to have some idea of how it works.

subtle remnant
#

sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = INADDR_ANY;

bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));

template <typename T, typename M_T, auto M>
constexpr M_T get_struct_value(const T& obj)
requires std::is_same_v<std::remove_cvref_t<decltype(std::declval<T>().*M)>, M_T>
{
return obj.*M;
}

int SendImage(const char* image) {
constexpr static auto socket_ = get_struct_value<serverAddress, int, &serverAddress::sin_port>(s);
//Some way to add the socket_ to the new struct
struct Sockaddr_in_file {
socket_type;
};
}
I havent checked or compiled it

#

And Im in class so its pretty bad

#

But hopefully you sort of get the premise

#

Even though its error prone and unclear

#

@stray wharf

#

You can just run it through AI and correct it

#

If needed

stray wharf
#

serverAddress is a runtime value.

subtle remnant
#

That is true yes

#

Ill have to reconsider my approach