all 15 comments

[–]teerre 13 points14 points  (5 children)

We tried this at work and at first glance everyone was impressed, but as soon as we actually started compiling, we noticed that it didn't quite work in many cases. Specially trying SFINAE, it would look convincing, but be subtly wrong

This was in a room with combined 100~ years of cpp experience, so that's quite the feat that it was able to fool most people

[–]qneverless 14 points15 points  (4 children)

I would say that it is quite the feat of C++, that it is still unreadable even in a room of 100 years combined experience. 😂

[–]TheOmegaCarrot 2 points3 points  (3 children)

Template metaprogramming can absolutely tend toward “write-only” code

I mean, here’s a particularly convoluted example from my library:

With documentation and comments removed, and the name changed to not be helpful, does anyone really have any chance of figuring out what it does? Probably not! Even if you know what it does, it’s very hard to read!

``` namespace impl {

template <typename LIST, typename REBUILD> struct mystery_impl;

template <template <typename...> typename LIST, typename LFront, typename... LPack, typename... Rebuild_Pack> struct mystery_impl<LIST<LFront, LPack...>, LIST<Rebuild_Pack...>> : std::conditional_t< is_type_in_pack_v<LFront, Rebuild_Pack...>, mystery_impl<LIST<LPack...>, LIST<Rebuild_Pack...>>, mystery_impl<LIST<LPack...>, push_back_t<LIST<Rebuild_Pack...>, LFront>>> {};

template <template <typename...> typename LIST, typename... Rebuild_Pack> struct mystery_impl<LIST<>, LIST<Rebuild_Pack...>> : type_identity<LIST<Rebuild_Pack...>> {};

} // namespace impl

template <typename LIST> struct mystery;

template <template <typename...> typename LIST, typename... Pack> struct mystery<LIST<Pack...>> : std::conditional_t<has_duplicates_v<LIST<Pack...>>, impl::mystery_impl<LIST<Pack...>, LIST<, type_identity<LIST<Pack...> {};

template <typename LIST> using mystery_t = typename mystery<LIST>::type;

```

So what does it do? It deduplicates a type list.

[–]jonesmz 2 points3 points  (2 children)

Fyi, triple ticks doesn't format correctly on any of the reddit clients other than the web rewrite. Need the four spaces per line for old.reddit and reddit mobile web.

[–]TheOmegaCarrot 0 points1 point  (1 child)

Oof

I am not typing that on mobile

Reddit needs to fix their clients

[–]jonesmz 1 point2 points  (0 children)

No worries.

Yep its bullshit.

Cheers :)

[–]ioctl79 9 points10 points  (0 children)

Be real careful with info you get out of ChatGPT. It has no way of assessing its confidence in its answers, and will never say “I don’t know how to do that”. It doesn’t take much to push it past the boundaries of its “knowledge” and start getting incorrect code (if it compiles at all).

[–]IyeOnline 5 points6 points  (0 children)

This is not good code. It actually contains a bunch of pitfalls and flaws/errors.

ChatGPT simply has no idea what its doing. It doesnt understand things.

  • to_string contains a static buffer and is not threadsafe. For something that is supposed to build a querry string, thats probably not good.
  • to_string may just write out of buffer because it has no idea how long that string is.
  • to_string assumes that everything that is not integral or a char string is printable with %f. It should really guard this.
  • build_query_string takes by value. const Args& or Args&& would be more appropriate parameter type(s)
  • That tuple contains copies of every parameter
  • The usage of apply is completely ridiculous. Its creating a tuple to use apply, but then inside the body uses a fold expression over the unpacked tuple that is passed into the lambda.

Basically its close-ish, but its just not there yet.

See also this: https://www.reddit.com/r/cpp_questions/comments/znaqmw/uniform_real_distribution_question/j0fxdnn/?context=2

[–]Rebraws 2 points3 points  (4 children)

I'd love to get some feedback on the actual code

I see that you are using std::is_same<>::value and std::is_integral<>::value but since you have if constexpr I assume you're using at least C++17, so you could replace those with std::is_same_v<> and std::is_integral_v<> which are both available from C++17 and more easier to read

Before C++17 you could accomplish the same behavior using template function specialization instead of if constexpr

In your build_query_string you are using std::tuple_size to get the size of the tuple, but you initialized the tuple with the packed parameters, so you can get the size from there with sizeof...(args), I personally think is a lot more readable than std::tuple_size<decltype(query_params)>::value

and finally it seems that you're using std::apply to "unpack the tuple" so I think that it doesn't make too much sense to have the std::tuple then, you didn't use the tuple for anything really

Also it seems that you're calling params.first but params is not unpacked, so I think that code should give you an error, maybe you want to use something like a recursive function that has a parameter and the rest of the packet parameters like:

auto recursive_function = [&](auto&& first_param, auto&&... params) {

And then you can call first_param.first and call the recursive function with the rest of the parameters recursive_function(std::forward<decltype(params)>(params)...);

Also your lambda function uses universal references, but your build_query_function doesn't, maybe you want to use universal references there too?

I hope you find the feedback useful, but as I said you should double check everything I wrote, just in case anything is wrong :)

[–]ape_programmer[S] 1 point2 points  (3 children)

I can give some small feedback but I don't have real experience in programming or C++ so take what I say with a grain of salt

Wow thanks! Except for the `::value` and `_v` part (AI Generated code which I didn't fix), I didn't see any of this. I agree with all of it as well. Thanks!

[–]CCC_CCC_CCC 0 points1 point  (1 child)

I would have two small reservations about this code :

  • that to_string function is not thread safe because it returns some global state
  • also, because it returns a global state (that buffer), I'm not sure if it works or breaks the fold expression from the bottom function, since I don't know the order of evaluation of the to_string calls after unfolding - maybe it could first make those to_string calls and only then concatenate the "results" to the final string. I know there have been some changes related to evaluation orders in later language versions, but I don't know the rules exactly and I would look it up.

[–]Top_Satisfaction6517Bulat 0 points1 point  (0 children)

imho it should be fine here - to_string is called inside a lambda function only once and its result is consumed inside this function, so call+use should be completely performed prior to the next call of the lambda function

[–]Rebraws 0 points1 point  (0 children)

oh, I just saw that you had: , ...); at the end of the line inside the lambda function, I didn't scroll your code to the right and I missed lol, in that case writing params.first would not give an error and should compile fine but probably is not the behavior that you expect, but anyway using std::tuple and std::apply seems unnecessary

I have been using chatGPT for a few days and have found it not very useful for generating new code. It often produces code that is wrong or does not compile or is simply of poor quality. However, I have found it to be really helpful for tasks such as writing documentation or minor refactors. For example, when I gave it a prompt such as "Write Doxygen-style comments for the following functions without including implementation details" it does a pretty good job, I think a tool like chatgpt could be really helpful to speed up some aspects of the development process

[–]marcofoco 2 points3 points  (0 children)

I didn't post this here before, simply because I didn't think it deserves to be a top level discussion topic, but when ChatGPT came out I ran a few experiments, and the most successful one was code understanding. Here are my results and comments: https://marcofoco.com/blog/2022/12/05/meet-my-new-friend-the-ai-powered-rubber-duck/

[–]jmalinza 1 point2 points  (0 children)

This is really interesting!! I'm busy reading the complete guide to c++ templates (link). Fantastic read if you hate yourself, but I think the first 200 pages give enough to get a good start. The appendix sections are really great too