#Get Item From Struct at compile time
73 messages · Page 1 of 1 (latest)
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.
Need to be more specific.
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?
Kind of, but what you are looking for is probably a member pointer.
Source ref so I can read?
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
Would need further elaboration then.
;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?
a = 42
template <typename T, auto M>
auto get_struct_value(const T& obj) {
return obj.*M;
}
This could work
(can also be made compile time with lite dusting of constexpr)
But it dosent check the variable
Check it for what?
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
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;
}
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
Still not sure what that would be used for.
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
Unfortunately I still don't.
sockaddr_in has values needed to communicate with the network
So I would for example pull the in_addr and socket
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
Parsing where?
Say I have a variable sockaddr_in address = getAddress(); how would your thing be better than just address.sin_addr?
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
Demonstrate with some code.
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
Pseudo-code is fine, I just need to have some idea of how it works.
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
That is not valid code.
serverAddress is a runtime value.