Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
matti
Mar 31, 2019

After trying getting into C++ multiple times before I recently started reading The Design and Evolution of C++ (1994) and I'm having great time just messing with the language. C++98 feels lot more like an extension to my beloved C instead of a horrid mess of templates and incomprehensible standardese (which I assume I'll learn to appreciate once I understand its motivations).

code:
//
// vbase.cpp -- Breaking virtual inheritance chains
//

#include <cstdio>

/*
 * Desired inheritance hierarchy:
 *
 *     A       A
 *    / \     / \
 *   /   \   /   \
 *  B1   B2 B1   B2
 *   \   /   \   /
 *    \ /     \ /
 *     C1     C2
 *      \     /
 *       \   /
 *        \ /
 *         D
 *
 */

//
// Assume this is code we can't modify.
//

class A {
protected:
        int v;

        A(int x) : v(x) {}
};

class B1 : public virtual A {
protected:
        B1(int x) : A(x) {}
};

class B2 : public virtual A {
protected:
        B2(int x) : A(x) {}
};

class C1 : public B1, public B2 {
protected:
        C1(int x) : A(x), B1(x), B2(x) {}
};

class C2 : public B1, public B2 {
protected:
        C2(int x) : A(x), B1(x), B2(x) {}
};


//
// Solution
//


//
// If we just inherited C1 and C2 they'd use the same instance of A. Instead
// declare them as member variables.
//

class D {
        friend void print(const D&);

        //
        // Wrap C1 and C2 and declare this class a `friend' to access their
        // protected members.
        //
        // Friend declarations of D have to be repeated as needed.
        //

        class C1 : public ::C1 {
                friend class D;
                friend void print(const D&);

                C1(int x) : A(x), ::C1(x) {}
        };

        class C2 : public ::C2 {
                friend class D;
                friend void print(const D&);

                C2(int x) : A(x), ::C2(x) {}
        };

        // Qualifier is unnecessary but helpful.
        D::C1 c1;
        D::C2 c2;

public:
        D(int x, int y) : c1(x), c2(y) {}

        void pow2()
        {
                c1.v *= c1.v;
                c2.v *= c2.v;
        }
};

void print(const D& d)
{
        std::printf("c1.v=%d, c2.v=%d\n", d.c1.v, d.c2.v);
}

int main()
{
        D d(2, 3);
        d.pow2();
        print(d); // Outputs "c1.v=4, c2.v=9"
}
(Obviously you'd avoid needing to do any of that in the first place)

e: im the friend declaration of D 👀
ee: drunkem typos

matti fucked around with this message at 19:25 on Aug 31, 2020

Adbot
ADBOT LOVES YOU

matti
Mar 31, 2019

code:
// bool test_safe_range(long value);
// bool test_safe_range(long long value); [C++11]
//
// Return whether `value' is in the range where any arithmetic operator is
// guaranteed to not cause overflow on any target platform.
//
// FIXME: I actually have the feeling this isn't safe as a predicate for
// using an operator (the conditional block asserts that this always returns
// true). Bignum algorithms may be required. See regehr.org.

inline bool test_safe_range(long value)
{
        return value <= SAFE_LONG_MAXIMUM && value >= SAFE_LONG_MINIMUM;
}
I'm little fried right now but can someone double check that I'm not about to overcomplicate this?

SAFE_LONG_MAXIMUM and SAFE_LONG_MINIMUM are sqrt(2^31-1) and -sqrt(2^31) respectively

e: Is the MSVC optimizer less aggressive in this regard? Then I could just use GNU builtin functions where needed.

matti fucked around with this message at 15:56 on Sep 17, 2020

matti
Mar 31, 2019

Captain Cappy posted:

Please rename the function can_be_squared_without_overflowing_a_thirty_two_bit_integer, thank you.

What are you worried about overcomplicating?

An aggressively optimizing compiler (I'm talking of GNU) is allowed to assume that signed integers never overflow and so may optimize out conditionals that'd check for it beforehand.

Now that I've had time to ponder it a bit more clearly (lol as if) I realize it's not an issue here and I'm home safe I think.

thanks for letting me rubber duck a lil

matti
Mar 31, 2019

is there any ICU #define to have it only expose the C API when compiling with a C++ compiler?

matti
Mar 31, 2019

#undef __cplusplus
# include <unicode/whatever.h>
#define __cplusplus

ah yea! thanks, works

matti
Mar 31, 2019

now onto writing a long comment block why the gently caress i need to mess with the compiler preprocessor symbols...

its Temporary™

matti fucked around with this message at 05:04 on Jan 28, 2021

matti
Mar 31, 2019

yeah i woke up with a little of a hangover and dutifully decided to not do that

matti
Mar 31, 2019

Question: I'm under the impression that C++ does not allow "static const" qualified variables to be modified, but does C have that feature?

Talking purely in terms of abstract machines, as defined by language standards.

matti
Mar 31, 2019

Hmmm, reading about it I think I have placed too much faith in random peoples bad interpretations of the language standard.

matti
Mar 31, 2019

Absolutely. I'm only thinking about this for documentation purposes. Got to be exact.

matti
Mar 31, 2019

-Wall -Wextra -Wno-unused

matti
Mar 31, 2019

learning the win32 api at long last (just to fix a bug in a software I want to use lol)

i think everyone first project is to write a function to report system library errors, here is mine.

which brings me to ask, how you do test for signed integer overflow when you use Microsoft's compiler? is there anything similar to GNU's __builtin_<op>_overflow() functions? or are you just supposed to make assumptions about widths of types and write your own in which you cast to the size next larger to test the result?

matti
Mar 31, 2019

i will take that as MSVC will not re-order my overflow checks.

nielsm posted:

(Or assume nobody's going to supply a message and format input exceeding 2 GB.)

hmmmm

matti fucked around with this message at 22:21 on Sep 29, 2021

matti
Mar 31, 2019

Foxfire_ posted:

intsafe.h is the MSVC equivalent of the gcc __builting_*overflow() functions [though looking at their implementations, they're undefined behavior/using secret knowledge about how the optimizer works and just doing the same overflow checks you would do]

thanks this is the knowledge i was looking for xoxo

matti
Mar 31, 2019

i ended up on the solution where i just write trivial inline assembly routines for signed integer addition when i have to do it

functions have the signature <op>_<types> or <op>_<type>_<to|by>_<type> and take a jmp_buf as a third argument for handling overflow.

only downside is having to declare pointers as volatile if you want to free them in the clean-up code. but it keeps any arithmetic very readable.

'unno, this is for personal stuff, i would not do this in an organization

matti fucked around with this message at 17:24 on Sep 30, 2021

matti
Mar 31, 2019

too new, maybe in couple of years

i do not typically expect anything to work

matti fucked around with this message at 05:54 on Oct 1, 2021

matti
Mar 31, 2019

i have a question about the core guidelines, specifically ES.87 "Don’t add redundant == or != to conditions"

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-if

many C style guides recommend the opposite, i.e. always use explicit comparisons unless the value is boolean. the guideline does not explain what "opportunities for mistakes" redundant comparisons have. is it just a misplaced style rule or are there realistic traps explicit comparisons have in C++?

Adbot
ADBOT LOVES YOU

matti
Mar 31, 2019

giogadi posted:

Some very hardcore people believe in including things in an order that doesn’t require guards. It’s insane to me but it’s a thing

rob pike has a lot of clout

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply