🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Size doesn't really matters...right?

Started by
54 comments, last by JoeJ 6 years, 9 months ago
5 minutes ago, JoeJ said:

let's write a pathtracer with 19 lines. :D

You will have difficulties beating the 99 line C++ Path Tracer which is unbiased, contains reflective/refractive/diffuse materials, uses a tent filter, uses multi-threading and is still somehow configurable.

🧙

Advertisement
14 minutes ago, matt77hias said:

You could factor out the size-1 due to the zero indexing:



const std::string buildPalindrome (const std::string &st) {
    const size_t end_index = st.size() - 1;
    for (size_t n = 0, back = end_index; n <= end_index; ++n) {
        if (st[n] == st[back]) { // found one more
            --back;
        }
        else { // reset search
            back = end_index;
        }
    }  

    return (back < 0) ? 
      st : st + std::string(st.rbegin() + end_index-back, st.rend());
};

 

You can't scope back inside the for() though, since you use it in the return :D (I know you know, just kidding hehe )

 

@JoeJ well done, timing wise is pretty much the same as mine, but if I knew how to do more accurate timing I bet your would win :)

Quote

You can write just  

bool const foo = true;

Unfortunately that wouldn't do it since the bool need to be able to be changed exactly once, otherwise there's no point in having it there :P

Quote

But there is no way to say variable will be set only once within a loop.

I need to find a way to bother the standard C++ Comitee xD

3 minutes ago, MarcusAseth said:

You can't scope back inside the for() though, since you use it in the return :D (I know you know, just kidding hehe )

I was faster^^ I used a web C++ compiler to double check :P (note that my latest edits are only related to the text below the code snippet)

3 minutes ago, MarcusAseth said:

but if I knew how to do more accurate timing

Here, you can find a Timer repository (Windows only), which provides a wall clock and CPU clock (kernel and user mode) timer. You probably want to use the latter.

🧙

49 minutes ago, MarcusAseth said:

@JoeJ well done, timing wise is pretty much the same as mine, but if I knew how to do more accurate timing I bet your would win :)

I assume the compiler figures out the redunant code you wrote, same for an optimization like predecrementing my size variable.

(But i never look at compiler output - i start crying so early)

1 hour ago, matt77hias said:

You will have difficulties beating the 99 line C++ Path Tracer which is unbiased, contains reflective/refractive/diffuse materials, uses a tent filter, uses multi-threading and is still somehow configurable.

Oh yes, i was referring to this http://fabiensanglard.net/rayTracing_back_of_business_card/, but that's just a raytracer.

2 hours ago, JoeJ said:

i was referring to this http://fabiensanglard.net/rayTracing_back_of_business_card/, but that's just a raytracer.

mm.png.93295a8ecfe45c7d76713e49b64f608d.png

;) (not "elite" though: 1360 bytes of code or 1350 bytes of code for a basic green color)

🧙

5 hours ago, MarcusAseth said:

const std::string buildPalindrome (const std::string &st)

Aehm... what means the first const for the return type?

I do const correctness just for a year and googling never gave me the answer :)

1 hour ago, JoeJ said:

Aehm... what means the first const for the return type?

I do const correctness just for a year and googling never gave me the answer :)

That basically prevents calling assignment operators (and other non-const methods) on your returned value.

For instance if you have some class Number and return a const Number for an overloaded operator*, the following will not be possible:

(a * b) = c;

This can avoid typos such as: if ((a * b) = c) instead of if ((a * b) == c).

So as a rule of thumb, always use const for returning by value of non-primitive, non-enum types. The latter are immutable by definition, so adding a const results in no added value. (See: Effective C++ of Scott Meyers)

 

As another rule of thumb, which is mostly ignored in code that I see, put the immutable value to the left when comparing against some possible mutable value:

if (5 == value)

or

if (5 == SomeExternalLibraryFunctionWhichDoesNotReturnAConstValue())

🧙

47 minutes ago, matt77hias said:

So as a rule of thumb, always use const for returning by value of non-primitive, non-enum types.

Is there an example where NOT returning value as const makes sense then?

26 minutes ago, JoeJ said:

Is there an example where NOT returning value as const makes sense then?

If you prefer to use something like (a-b).Normalize() over Normalize(a-b); . I clearly recommend the latter though. That is basically the only example I found so far (and returned pointers, references, primitives and enum values which are all in fact primitives).

🧙

3 hours ago, matt77hias said:

For instance if you have some class Number and return a const Number for an overloaded operator*, the following will not be possible:

(a * b) = c;

This can avoid typos such as: if ((a * b) = c) instead of if ((a * b) == c).

This seems wrong to me (unless your return type is a reference), the returned type from a function cannot be assigned to because it not a modifiable lvalue, regardless wether it has const or not.

That example still won't compile when trying to assign without const in the return type

This topic is closed to new replies.

Advertisement