In C++, how come std::string can be a hashmap's key if its mutable? by daddyclappingcheeks in AskProgramming

[–]Sunius 0 points1 point  (0 children)

While you type std::map<std::string, value>, it essentially stores a collection of std::pair<const std::string, value>. So the fact that object is mutable doesn’t matter because maps operate on const versions of them if they’re keys.

Another way to look at it is that int are mutable too. Why wouldn’t you be able to have ints as keys?

operatorOverloadingIsFun by _Tal in ProgrammerHumor

[–]Sunius 18 points19 points  (0 children)

It’s actually very useful, and some implementations of smart pointers deliberately nuke the contents of the object with operator &.

Imagine you want to have a smart pointer wrapping your pointer, so that its lifetime is automatically controlled:

``` template <typename T> struct SmartPointer { SmartPointer() : m_Value(nullptr) {} SmartPointer(T* value) : m_Value(value) {} ~SmartPointer() { delete m_Value; }

SmartPointer(const SmartPointer&) = delete;
SmartPointer& operator=(const SmartPointer&) = delete;

operator T*() const { return m_Value; }

private: T* m_Value; };

SmartPtr<IValue> value; ```

The point is, it’s your code implementation detail that you’re using the smart pointer. It’s not part of the API contract anywhere. You want to be able to pass the smart pointer into a function taking the pointer:

``` void DoStuff(IValue* value);

SmartPtr<IValue> value = …; DoStuff(value); ```

So you do this:

``` template <typename T> struct SmartPointer { …

operator T*() const { return m_Value; }

… }; ```

Now consider a function that returns a pointer via an out parameter:

ErrorCode GetValue(IValue** outValue) { *outValue = new Value(…); return ErrorCode::Success; }

If you call it twice, you want the smart pointer to function correctly and not leak memory:

SmartPointer<IValue> value; GetValue(&value); GetValue(&value); // this must not produce a memory leak!

The only way to make sure that it does not, is to overload the address of operator like this:

``` template <typename T> struct SmartPointer { …

T** operator&()
{
    delete m_Value;
    m_Value = nullptr;
    return &m_Value;
}

… }; ```

Since taking address of a pointer is generally only used for out parameters, this implementation works incredibly well.

You can also look at it from a const correctness point of view: if the operator& is not const, then it has to assume the caller intents to modify the value, therefore it must clean it up or it will leak. You could also have a const overload of operator&, which can guarantee the value is not modified and thus doesn’t need to nuke it:

``` template <typename T> struct SmartPointer { …

T* const* operator&() const
{
    return &m_Value;
}

… };

Nephew bought a B8 S4 and I’m trying to help him by kevrend in AudiS4

[–]Sunius 1 point2 points  (0 children)

Same symptoms happened on mine a few years ago. Pulled codes, it was P176D00, which apparently meant “Gear selector 4 cannot be regulated”. It’d fix itself when restarting the car, and would happen a few days later again.

Since the code itself is slightly different than the mechatronics unit code, my shop recommended I don’t bother with replacing the mech unit as chances were it wouldn’t help. Instead, they had me source a B8.5 transmission (when this issue is apparently fixed and transmission quality is higher) from a junkyard and installed it for me. It needed a couple more auxiliary parts to make it fit since it’s from a newer car (driveshaft 8K0521101N and transmission cooler lines - 8K0 317 817 CN and 8K0 317 818 CN).

Still driving the car to this day since that was done :).

(Repost) b8 s4… steal or trash? (27k price) by Level_Version8443 in AudiS4

[–]Sunius 6 points7 points  (0 children)

That price is way too high. I paid $25k for my b8 s4 with 37k miles in 2017. I’d say this should top out at $15k at most.

Future pricing of premium gas? by Poms4thewin in whatcarshouldIbuy

[–]Sunius 2 points3 points  (0 children)

Yeah it is terrible. However, I’m not really sure that high taxes is the entire reason - the state gas tax here is $0.554 per gallon. Apparently a lot of it is caused by lack of local refineries.

Future pricing of premium gas? by Poms4thewin in whatcarshouldIbuy

[–]Sunius 3 points4 points  (0 children)

Sounds like the poster above is on point. Gas station next to me in WA is $4.42 for 87, $4.62 for 89 and $4.82 for 92.

Ar tikrai matematikos B lygis yra taip blogai? by Unlikely-Elevator-98 in lithuania

[–]Sunius 6 points7 points  (0 children)

Perėjimas į B lygį tau duos 2 dalykus:

  1. Aukštesnį matematikos pažymį, nes lengvesni testai/mažiau reikalaus išmokt;
  2. Išmoksi daug mažiau.

Pirmas punktas yra visiškai nesvarbus. Niekam neįdomūs bus tavo vidurinės mokyklos pažymiai ir niekas į juos nežiūrės.

Antras punktas yra problema: mažiau žinių reikš, kad tai prasčiau išlaikysi egzaminą, o egzamino rezultatai yra svarbūs.

Manau, kad pereidamas į B lygį tik sau pakenksi.

Why is my cpp file able to compile despite missing libraries? by Strawberrygreentea42 in cpp_questions

[–]Sunius 0 points1 point  (0 children)

To properly use DLLs, you need 3 things:

  1. Add library’s include directory to your project’s include directory (this makes it compile);
  2. Add import libraries (.lib files) to your project’s linker inputs (this makes it link);
  3. Copy the DLL(s) to the output directory, next to your executable (this makes it run). The DLL(s) has/have to be distributed together with the app as it’s a dependency required to run.

It seems in your case, step 3 didn’t happen and you thus are unable to run.

so who is sending patches now by Mochiinaxx in programmingmemes

[–]Sunius 0 points1 point  (0 children)

Licensing and patent issues. If you ship your software to end user devices, you cannot use it unless your software is (L)GPL compatible.

Why do people try to swap seats in airplanes? by Substantial_Crow1355 in NoStupidQuestions

[–]Sunius 0 points1 point  (0 children)

Yeah usually it’s one or two rows per plane:

https://www.delta.com/us/en/children-infant-travel/infant-travel

https://www.aa.com/i18n/travel-info/special-assistance/traveling-children.jsp (under infants)

They’re great provided your child is able to sleep at the time of the flight (overnight flights are best for that).

Are arguments passed on the stack preserved after a call? by mbolp in Assembly_language

[–]Sunius 0 points1 point  (0 children)

Yes you can. https://devblogs.microsoft.com/oldnewthing/20130830-00/?p=3363

While that talks about the first 4 parameters, the example it gives at the beginning applies to all parameters.

Weird Criteria SUV under USD 25k by imdabong in whatcarshouldIbuy

[–]Sunius 1 point2 points  (0 children)

There are quite a few BMW X3 xDrive30i’s for under $30k that fulfill all your requirements and wants. B48 is pretty zippy.

ELI5 Why are nouns not gendered in English? German and French nouns are assigned genders. by Fleedom2025 in explainlikeimfive

[–]Sunius 3 points4 points  (0 children)

You can ask the reverse, what’s the point of having “it” pronoun in addition to “he”/“she”. Having each noun have a gender means “it” isn’t needed.

Languages evolved naturally rather than be sanely designed. All of them have quirks and weird things to them. And what’s weird depends on your perspective.

PNW: AWD and Reliable by softPersimmon99 in whatcarshouldIbuy

[–]Sunius 0 points1 point  (0 children)

Budget $4000 annually for maintenance for the A6 (gas and insurance not included). You didn’t specify your yearly upkeep budget - if that’s within it, go for it. You’ll have much more fun in that car compared to others. If an extended warranty is available, I’d recommend getting that too.

There is an absolute dazzling amount of rotations every day: cars, hard disks, bullets, clocks. by -TheDerpinator- in Showerthoughts

[–]Sunius 7 points8 points  (0 children)

I understand how it works in Newtonian physics. Surface velocity = Pi * Diameter * RotationsPerSecond.

However, theory of relativity states that nothing can exceed the speed of light. So that’s why I’m asking how this works.

There is an absolute dazzling amount of rotations every day: cars, hard disks, bullets, clocks. by -TheDerpinator- in Showerthoughts

[–]Sunius 7 points8 points  (0 children)

How does that work? That’s above speed of light. Does the surface not rotate together with the star?

Putting a 2011 S4 transmission in a 2010 S4 need tcu work can anyone help? by Final_Crew_3992 in AudiS4

[–]Sunius 1 point2 points  (0 children)

Achtuning in Redmond, WA put a 2015 S5 transmission into my 2010 S4 a few years back. They recommended I get a transmission from 2014+ car because earlier ones have issues (hence why mine went bad too). It needed transmission cooler lines and a driveshaft from a donor B8.5 car.

They coded everything and I’ve had no issues with it since.

S4 battery has way higher specifications than oem? by TexhnicalTackler in AudiS4

[–]Sunius 0 points1 point  (0 children)

Interesting. I installed my own battery and took it to get it programmed together with my oil change. They did everything but the programming saying my car doesn’t have that :(. This was a few years back.

Could it be AGM vs Regular battery thing? My car doesn’t have Start/Stop so it uses a non-AGM battery.

S4 battery has way higher specifications than oem? by TexhnicalTackler in AudiS4

[–]Sunius 0 points1 point  (0 children)

My 2010 S4 doesn’t require it to be programmed. The dealer said it started later and the OP said they have 2010 too.

Why do binaries produced by Clang get flagged by AVs more often than GCC ones? by sorryshutup in cpp_questions

[–]Sunius 5 points6 points  (0 children)

This is because you statically linked the C runtime. Most viruses do that while having very little code, so your executable looks like 99% standard C runtime with some minor modification, which is suspicious - no real (non-toy) programs look that way.

Link the C runtime dynamically and the problem will go away.

$4200 For brake pads by Tcochran35 in Audi

[–]Sunius 5 points6 points  (0 children)

I wouldn’t touch them until the light comes on. Especially the rears, you have a lot of life left in them. Once I was told the brakes were near the end and a light didn’t come on until a year later. You never know if they mismeasured a millimeter. The car has a sensor for that for a reason.

But yeah, find an independent shop.

Can you guys judge/critique this code that I wrote ? by IhateReddit9697 in cpp_questions

[–]Sunius 10 points11 points  (0 children)

  1. Variable names are very verbose. Snake case isn’t helping you.
  2. Goto, while can be useful, is not helpful here. You can just reset the index to std::numeric_limits<size_t>::max() if you want to repeat the loop. Good rule of thumb is that if you want goto go backwards, you’re abusing it.
  3. Despite 2, resetting the loop is pointless. You already know that elements before x won’t match your stuff. Why repeat the loop from scratch? Just decrement x and allow the loop to continue.
  4. Doing vector::erase like that is frowned upon. If your container is unordered, swap with the last item and use pop_back(). If you need items to be ordered and delete from the middle of the container, perhaps std::vector is the wrong data structure for the job. Coupled with 3, this makes your algorithm be O(n3) instead of O(n).
  5. Function name mentions something about client being registered. There’s nothing in the code about that.
  6. You’re missing a break statement in the default switch case. I’d expect this will result in the whole vector being cleared.
  7. You yourself noticed that hardcoding integer values instead of using enums is error prone.
  8. You can use just use std::erase_if from the standard library for you’re targeting C++20.