possibly-wrong/precision

Rational C-string constructor

Closed this issue · 4 comments

math::Rational("no std::string")
as opposed to
math::Rational(std::string("std::string"))

This would fix THIS MESS:

image

Do you even see how many std::strings there are?? It's crazy!

You could also do this for the other types, e.g. Unsigned and Integer.

You can construct a Rational("123/456") directly from a const char * argument (which will be implicitly converted to a std::string). You don't specify the type of your SYSTEM_TYPE, but in the case of a brace-initializer list, you don't have to specify the std::string(), just wrap each literal (meant to be implicitly converted to a Rational) in braces, like this:

#include "math_Rational.h"
#include <vector>

struct UNIT {
    UNIT(const math::Rational& value, const std::string& name) :
        value(value), name(name) {}
    math::Rational value;
    std::string name;
};

struct SYSTEM_TYPE {
    std::vector<UNIT> fluid;
    std::vector<UNIT> length;
    std::vector<UNIT> weight;
};

int main() {
    SYSTEM_TYPE imp_units {
        { // Fluid
            {{"1"}, "Minims"},
            {{"60"}, "Fluid drams"}
        },
        { // Length
            {{"1"}, "Thous"},
            {{"1000"}, "Inches"}
        },
        { // Weight
            {{"1"}, "Grains"},
            {{"875/32"}, "Drams"}
        }
    };
}

I can't use C-strings there because they aren't implicitly converted. SYSTEM_TYPE is std::vector<std::map<math::Rational, std::string>> and C++ only implicitly converts one level deep (i.e. I don't have to include the math::Rational constructor, but I have to include the std::string passed to it). It doesn't try to figure out that C-Strings are passed to std::string which are passed to math::Rational, in my experience.

Look more closely at my example (re-pasted here and modified to use your SYSTEM_TYPE):

#include "math_Rational.h"
#include <vector>
#include <map>

typedef std::vector<std::map<math::Rational, std::string> > SYSTEM_TYPE;

int main() {
    SYSTEM_TYPE imp_units {
        { // Fluid
            {{"1"}, "Minims"},
            {{"60"}, "Fluid drams"}
        },
        { // Length
            {{"1"}, "Thous"},
            {{"1000"}, "Inches"}
        },
        { // Weight
            {{"1"}, "Grains"},
            {{"875/32"}, "Drams"}
        }
    };
}

You are correct that {"875/32", "Drams"} doesn't work, but {{"875/32"}, "Drams"} does.

It does? Wow. I guess I'll make that change, thank you.

EDIT: Also, looks like you use CLion (or another IntelliJ IDE), so yay! Same-IDE buddies! (Unless I'm wrong)