M
M
Mercury132016-04-16 14:02:00
C++ / C#
Mercury13, 2016-04-16 14:02:00

How justified is C++ code in Perl/PHP style with the || in the role of if (!)?

Let's say we have a function

bool tryParseFloat(const wchar_t* aData, const wchar_t aPoint, double& rValue) {}

Such code is common in Perl or PHP, but is it expected in C++?
tryParseFloat(data, L'.', value) || tryParseFloat(data, L',', value);

The example is synthetic and not entirely successful, but the principle was understood.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maaGames, 2016-04-16
@maaGames

You shouldn't write like that. The optimizer may well precalculate function arguments before calling the methods themselves, and if the arguments are not trivial, then this may change the program logic. And in general, this is quite difficult to read and not obvious.

A
AtomKrieg, 2016-04-16
@AtomKrieg

//! Brief description.
//function try to parse with aPoint decimal delimiter using position order in vector
//in example: if aPoint[0] fails try aPoints[1] etc...
bool tryParseFloat(const wchar_t* aData, const std::vector<wchar_t>& aPoint, float& rValue) {}

tryParseFloat(data, {L'.', L','}, value);

//или
bool tryParseFloat(const wchar_t* aData, const wchar_t* aPoint, float& rValue) {}

tryParseFloat(data, L".,", value);

But I would make two such signatures (it is not always necessary to check the result of parsing):
float tryParseFloat(const wchar_t* aData, const std::vector<wchar_t>& aPoint, bool& result) {}
float tryParseFloat(const wchar_t* aData, const std::vector<wchar_t>& aPoint, float default_value = 0.0f) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question