C++ Review Questions

Pages: 1... 678910
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ranges>
#include <concepts>
#include <list>
#include <vector>

namespace utility
{
    template < typename T > // output_streamable type - an object of type T can be sent to std::ostream
    concept output_streamable = requires(std::ostream & stm, const T & v)
    { { stm << v } -> std::same_as<std::ostream&>; };

    template < std::ranges::range RANGE >
        requires output_streamable<std::ranges::range_value_t<RANGE>> ||
                 std::ranges::range<std::ranges::range_value_t<RANGE>>
    std::ostream& operator<< ( std::ostream& stm, const RANGE& range )
    {
        stm << +"[ "; // + to avoid ambiguity with operator in std namespace
        for( auto&& v : range ) stm << v << ' ';
        return stm << ']';
    }
}

int main()
{
    const std::list< std::vector< std::list<int> > > lists
    { { {0,1,2}, {3,4} }, { {5,6}, {7} }, { {8}, {9,10,11,12} }, { {13,14}, {15,16} } };

    using utility::operator<<;
    std::cout << lists << '\n';
}

https://coliru.stacked-crooked.com/a/63f6745286d4a2a1

TO DO: add support for maps (ie. add support for streaming pairs of streamable types)
This is so far off of where I was going I would never have gotten something even remotely workable. :)
That's modern C++!
seeplus wrote:
That's modern C++!

I was asking for a glass of water, overloading operator<< for a 2D vector or 2D list, and I got a fire hose on full.

I'm not complaining -- really, I'm not -- am more than a bit overwhelmed since most of the code is brand-new to me. C++20 is so feature-rich and I'm a slow learner.

It was only a couple of days ago I saw a SO thread about using C++20 Concepts for making a templated function be generic for select C++ containers and modified an old templated operator<< for a 1D vector<T>.

Now it is up to me to play with and modify that code so I better understand the nuances of what it is doing. :)
It is just a preliminary sketch, to illustrate an idea. As it stands, we would get ambiguities for ranges that are supported in namespace std (for example for std::string).
Now it is up to me to play with and modify that code so I better understand the nuances of what it is doing. :)


That's how to learn.

But its one thing to to be able to understand (and modify) some code, it's quite another to come up with the workable code in the first place....

That separates the Druids from the Wizards...
Last edited on
JLBorges wrote:
It is just a preliminary sketch, to illustrate an idea.

The code is 21st Century technology, and I'm 5th Century CE.

I wish I could classify myself as even a lowly "Expert Beginner...." But, alas, that is above "my paygrade." I am still at the Novice stage.

I read this several years ago, https://daedtech.com/how-developers-stop-learning-rise-of-the-expert-beginner/
I saw a SO thread about using C++20 Concepts for making a templated function


I'd go so far and say that concepts and ranges are the big game-changers in C++20 - and possibly the biggest since C++11...

I am still at the Novice stage.


Don't undersell yourself! :)
seeplus wrote:
Don't undersell yourself! :)

I can be rented, real cheap, though. :D
seeplus wrote:
I'd go so far and say that concepts and ranges are the big game-changers in C++20 - and possibly the biggest since C++11...

I've said it before, C++20 is arguably as big a structural change to the language as C++11 is.

I'm not complainin', mind you. Just beginning to notice the definite emphasis shift in how things can be (and should) crufted.

I can imagine C++340 program would be as simple as
1
2
3
Our stuff;

Do this(a);

And out will pop a fully functional app that does everything.
QUESTION 12)
12A) Any idea why I can't access the set'ers of this 3rd party Sprite class via the iterator, but I can access the get'ers via the same iterator just fine? I can access the set'ers via array [] subscript though. I tried making my own class & trying the same & it works just fine with my own.

12B) What container do you think I should use?

1) vector- constant time insertion/deletion at end.
2) deque- constant time insertion/del at end & beginning
3) list- constant time insertion/del anywhere, linked list
4) forward_list- const time insertion at front of list & going forward, not sure about the deletion though but it is probably constant front del too.
5) set- quick & frequent find/search via key (key is the data)
6) map- - quick & frequent find/search via key relative to data

Now, I think I may likely do frequent searches based on x position eventually, BUT the x position will change constantly & this cannot be used as key to lookup via set & map...because you cannot change them if they are used as keys in set & map. Also, I am not worried about insertion time as all that will be done at start of program, but more quick access & manipulation time of the data in the class, so I guess vector or list is just fine. I could probably easily just do it with a * new Sprite []/delete[] too.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    vector<Sprite>::const_iterator iter = scalesSprites.cbegin();
    int xGrowFactor = 0;
    for (iter; iter != scalesSprites.cend(); ++iter, xGrowFactor += 150)
    {
        //(*iter).setTexture(texture);        //NOT WORK
        //iter->setTexture(texture);          //NOT WORK
        iter->getTexture();                   //WORKS
    }

    //WORKS for access with array [] subscript
    for (auto i = 0; i < scalesSprites.size(); ++i, xGrowFactor += 150)
    {
        scalesSprites[i].setTexture(texture);
        scalesSprites[i].setScale(.5, .5);
        scalesSprites[i].setPosition(xGrowFactor, 800);
        
    }



12C) What main C++ are the rest of you reading/have read? How much time have you spent on it & how long did it take you to learn it? How long do you think you will have to spend on the C++23 future book?
Last edited on
const_iterator means you can't modify any values via the iterator. A setter modifies. A getter theoretically doesn't modify.

Your regular for loop doesn't set the vector to const so you can mash on the container's contents all day and night long.
Last edited on
Hmmm, it is all or nothing then. Thought it might mean you cannot add/remove elements, but not only that, you cannot change any of the data members in a class. Even if there is tons of them & you wanted to block off (const) one of them & make changes to the rest, you can't. Got it, thanks.
With const you can't pick and choose, it is all or nothing.

https://en.cppreference.com/w/c/language/const

Enjoy wading through that all.
There are ways around const, const_cast conversion. Hackish, but it is part of C++

https://en.cppreference.com/w/cpp/language/const_cast

Still not a good idea:

ES.50: Don’t cast away const
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-const
Last edited on
ES.71: Prefer a range-for-statement to a for-statement when there is a choice
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-for-range

1
2
3
4
5
6
7
8
9
10
11
12
std::vector<Sprite> scalesSprites ;
// ...

for( const Sprite& s : scalesSprites )
{
    // const: can't modify Sprite s
}

for( Sprite& s : scalesSprites )
{
    // non-const: can modify Sprite s
}
Since C++20 a range-based for loop can have an init statement to declare a variable within the scope of the for loop.

https://en.cppreference.com/w/cpp/language/range-for

Declaring xGrowFactor in a range-based for loop in an init statement is a possibility. You then increment it within the for control block.
I like range-based-for also, but I have been avoiding them because they are too easy :)
@George P,
Remember that good old President singleton in Sam's book, well today I tried to put it in header/file (not ready for modules quite yet) & it worked fine. Only there was a green squiggly line underneath the operator=. When I hoover over the squiggly it says, "Function definition for 'operator=' not found!".

Well, the book specifically says you don't have to define it, just declare it is good enough...it is a Singleton for crying out loud. Alright, so lets see what happens when I put it in... I put it in the definition as empty & then I get an error..."'operator=' must be a member function"

Everything works fine though with no definition, so going back to no defintion of it is there anyway to get rid of that green squiggly line?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <string>
using namespace std;

class President
{
	President() {}; //private default constructor
	President(const President&); //private copy constructor
	const President& operator = (const President&); // assignment operator

	string name;

public:
	static President& GetInstance();

	const string GetName() const;

	void SetName(string inputName);

};



Last edited on
Pages: 1... 678910