0

this is a VERY simplified version of the question to make it obvious what i am asking. I cannot seem to find it on Stack Overflow but i am probably searching using the wrong words!

Here is a Template Class with the obvious parts removed.

template <class T, bool clip = true>
class BOUNDED_VAL { 
    public:     
        BOUNDED_VAL(T initialMin, T intialMax) :locked(false) {     
            assert_or_throw(intialMax >= initialMin, HD_ERR_MINMAX_REVERSED);
            min = initialMin;
            max = intialMax;
            value = initialMin;
        };etc.

// assert_or_throw is a typedef which asserts during debug builds to prevent programmer mistakes (especially my own) or throws a rich catachable runtime error for user input just in case something gets around user input limit checking during release builds (A hard wall). Belt and braces approach...

Now i know i can set this up as an initialised private class member variable like so:

private:
    BOUNDED_VAL<int> testBoundInt = BOUNDED_VAL<int>(0, 10);

BUT

  1. Does this create a new BOUNDED_VAL and then copy it over the member variable (or compiler smooshes this away during optimisation)?

  2. Is there a more succinct way of doing it which i am just not finding? I know the following do not work but for example:

private:
    BOUNDED_VAL<int> testBoundInt(0,10);

or

private:
    BOUNDED_VAL<int>(0,10) testBoundInt;

I am self taught in C++ so it might be an obvious question...but you never know...

Many Thanks

KayAnna
  • 1
  • 1
  • 2
    Relevant: [Initialization of member variable via parentheses doesn't work](https://stackoverflow.com/q/71514176/580083) – Daniel Langr Jan 18 '23 at 11:55
  • this is not specific to the members type being an instantation of a template, you get same errors when the member is an `int` https://godbolt.org/z/MhqbKjGGx. (btw the same is true for templates rather often, `BOUNDED_VAL` is just a type like any other, only rarely it matters that its an instnatiation of a template) – 463035818_is_not_an_ai Jan 18 '23 at 12:31

1 Answers1

1

I know the following do not work but for example:

private:
   BOUNDED_VAL<int> testBoundInt(0,10);

But this will work, provided your compiler was written in the last decade:

BOUNDED_VAL<int> testBoundInt{0,10};

It looks like you might be using an outdated textbook to learn C++ that does not cover uniform initialization syntax from the current version of C++. You are strongly encouraged to get updated learning material.

Does this [copy initialization] create a new BOUNDED_VAL and then copy it over the member variable (or compiler smooshes this away during optimisation)?

This actually depends on the C++ version your compiler supports, and is configured to use. Depending on several factors it gets "smooshed" away, or we take a scenic trip to the countryside, where an object gets constructed, then a second object gets copy-constructed, and the first object deleted.

But this is now a secondary topic, since uniform initialization syntax solves the original problem.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Brilliant, thanks sooo much. I honestly had not seen the braces version used before...and my textbook is....the internet/google so learning material is...somewhat chaotic whenever i encounter a specific problem i am trying to solve. – KayAnna Jan 18 '23 at 12:10
  • "Internet/google" is a valuable tool for looking up stuff that you know exists, somewhere, and only needs to be located. But it is not a replacement for a textbook. Admirable progress can be made using solely a search engine, and a brilliant mind, but even a brilliant mind will not be able to accurately reassemble about two thousand pages of terse, typewritten text, that comprises the current version of the C++ standard, based on clues scattered across the third planet from the Sun, using merely a search engine and nothing else. – Sam Varshavchik Jan 18 '23 at 12:13
  • Is there a specific book/set of books you would recommend? – KayAnna Jan 18 '23 at 12:18
  • @KayAnna Here's a great list https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. I would like to add Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14 by Scott Meyers (did not see it in the list) – franji1 Jan 18 '23 at 12:31