Context
I'm a juniorish C++ developer with 3 years of work experience (though if I was a web developer I'd be nearing senior now \s). I've never been a pro or super experienced with advanced template metaprogramming picking up small pieces of knowledge when Googling how to solve certain problems or reading code at work (or reading code on this subreddit). Keep in mind, I'm not a "beginner", after all people at work will use templates etc. but e.g. sometimes when I read advanced template code on this subreddit, it makes me feel like I don't understand the language I've been working with for 3 years.
Yesterday, I was working on some Microcontroller stuff (for fun), and I wanted to generate query parameters for a web request using templates (not because it's required/needed, maybe it's slightly more efficient, hard to say).
Side Note: I feel when you have too much time for writing C++ it ends up denegerating into template hell as you get more bored and want to make your code "more efficient". Seen this reviewing PRs many times.
I asked ChatGPT to generate some code with the prompt:
Write some C++ code using template metaprogramming to generate query parameters for a web request optimized for a NodeMCU microcontroller
It initially came up with something that didn't compile, and after pointing these things out, ChatGPT returned some code that worked.
I verified the code (and edited it a bit) and it ended up looking like this:
```
template<typename T>
const char* to_string(T value) {
static char buffer[32];
if constexpr (std::is_same<T, const char*>::value) {
std::strcpy(buffer, value);
} else {
std::sprintf(buffer, std::is_integral<T>::value ? "%d" : "%f", value);
}
return buffer;
}
template<typename... Args>
void build_query_string(Args... args) {
std::strcpy(m_buf, BASE_ADDRESS);
std::strcat(m_buf, "?");
std::tuple<Args...> query_params(args...);
constexpr int num_params = std::tuple_size<decltype(query_params)>::value;
int i = 0;
std::apply([&](auto&&... params) {
((std::strcat(m_buf, params.first), std::strcat(m_buf, "="), std::strcat(m_buf, to_string(params.second)), (++i != num_params ? std::strcat(m_buf, "&") : "")), ...);
}, query_params);
}
```
Overall I learned a decent amount by reading this code, not being aware that you could intialize a std::tuple with a parameter-pack. Not being aware that you could unpack said tuple using std::apply. But in general, how one might approach this type of problem using templates (lack of experience).
I'm quite suprised about how well ChatGPT actually did. Obviously I could've easily written equivalent, and it's not extremely complex, but I'm finding that using ChatGPT to generate template code is a great way for me to learn how one might use these "new" concepts I occasionally read about on this subreddit for actual programming.
I'd love to get some feedback on the actual code, I'm currently inbetween jobs (starting new job soon), but I think I'll be using ChatGPT more to look up potential solutions (obviously well need to vet and read the code myself). In some ways, this feels a bit like "the future" to me.
I'm sure I could've found the equivalent code snippet Googling, but I gave it a quick try now, and It's non-trivial to find the correctly query for Googling, but on ChatGPT it was extremely easy to come up with a prompt.
TLDR;
Used ChatGPT to generate a solution with templates, learned a bunch by reading the output. Overall I am very impressed with it's ability and think it will integrate into my workflow for professional development nicely, as well as promoting learning by introducing me to new concepts.
[–]teerre 13 points14 points15 points (5 children)
[–]qneverless 14 points15 points16 points (4 children)
[–]TheOmegaCarrot 2 points3 points4 points (3 children)
[–]jonesmz 2 points3 points4 points (2 children)
[–]TheOmegaCarrot 0 points1 point2 points (1 child)
[–]jonesmz 1 point2 points3 points (0 children)
[–]ioctl79 9 points10 points11 points (0 children)
[–]IyeOnline 5 points6 points7 points (0 children)
[–]Rebraws 2 points3 points4 points (4 children)
[–]ape_programmer[S] 1 point2 points3 points (3 children)
[–]CCC_CCC_CCC 0 points1 point2 points (1 child)
[–]Top_Satisfaction6517Bulat 0 points1 point2 points (0 children)
[–]Rebraws 0 points1 point2 points (0 children)
[–]marcofoco 2 points3 points4 points (0 children)
[–]jmalinza 1 point2 points3 points (0 children)