You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace auto with explicit types in C++ for clarity, better error messages #139
I've done a fair amount of both C and C++ programming, but it's been a while. I've recently been trying to revitalize my C++ knowledge and so I did the first few exercises on the C++ track.
However, when I got to gigasecond, I had some issues. Whenever I tried to build my stub solution, it gave me a huge amount of notes and error messages. I spent a while looking into how to link to the boost library, googling the errors I was getting, etc. etc. without getting anywhere.
Finally I realized that my function was returning a string, but the test case was expecting a boost::posix_time::ptime. This would have been immediately apparent if the test case explicitly stated the return type as a ptime. Instead, it used the auto keyword. This saves a few characters in the test case but cost me a lot of time. Maybe this isn't an issue for most, I would bet that it's happened before or will happen in the future.
How often is auto necessary? Are there some times when we give the user the freedom to use whatever type they want? This would require there being (or the user providing) a == function to compare their output type with the test case output type. This seems like it would rarely, if ever, be the case.
If all of these things I true, I believe it would be reasonable to encourage writers of test cases to avoid auto as much as possible and to revise the test cases that currently use auto to use explicit types instead.
Am I missing anything? Or am I being unreasonable? Hopefully I'm not just cranky and tired :)
The convention for many of the test suites is to use auto for the user return values, but as far as I'm aware all of the expected values are either explicitly typed or rather obvious (like a string that we compare against directly, as in BOOST_REQURE_EQUAL("expected", user_function());).
BOOST_AUTO_TEST_CASE(test_1) {
const auto actual = gigasecond::advance(time_from_string("2011-04-25 00:00:00"));
const ptime expected(time_from_string("2043-01-01 01:46:40"));
BOOST_REQUIRE_EQUAL(expected, actual);
}
That said, the template error log always ends up being both an annoyance and a necessary evil for C++, and learning how to deal with it can be a good thing. Usually what you want to look at is the first error that gets spit out, but not always.
You hit a particularly bad corner case though. Comparing using Boost Test, between a Boost class and a standard class, all of which are templates all the way down? That sucks. At a minimum changing the first auto to ptime in the gigasecond test suite is probably a good idea, and if you want to open a PR with that I'd be happy to merge it 👍
If you're busy, I can get to it myself at some point.
Activity
arcuru commentedon Jul 29, 2017
The convention for many of the test suites is to use
autofor the user return values, but as far as I'm aware all of theexpectedvalues are either explicitly typed or rather obvious (like astringthat we compare against directly, as inBOOST_REQURE_EQUAL("expected", user_function());).That said, the template error log always ends up being both an annoyance and a necessary evil for C++, and learning how to deal with it can be a good thing. Usually what you want to look at is the first error that gets spit out, but not always.
You hit a particularly bad corner case though. Comparing using Boost Test, between a Boost class and a standard class, all of which are templates all the way down? That sucks. At a minimum changing the first
autotoptimein the gigasecond test suite is probably a good idea, and if you want to open a PR with that I'd be happy to merge it 👍If you're busy, I can get to it myself at some point.