Code Analysis/Critique

Pages: 12
No nothing like that. You can change the standard that it's compiling in itself and it does make suggestions sometimes like when you're defining a macro but other than that it doesn't suggest you to use better 'C++17 alternatives' if you may.

I guess you have to glimpse the new stuff in each standard and decide for yourself.
It sort of does. If you do something outdated, you will get a 'deprecated feature' warning. I think most compiles do this.
But most of the "deprecated feature" warnings from Visual Studio are bogus. For example almost all of the warning about using an "unsafe" function (ie: strcpy()) are incorrect. Most of those functions are perfectly valid standard functions.
I forgot about that! They tried to warn about functions that can be exploited by hacks, I think, and ended up making an unholy mess.
Exploited by hacks!?
closed account (E0p9LyTq)
For example almost all of the warning about using an "unsafe" function (ie: strcpy()) are incorrect.

If there are C standard equivalent functions available, and with strcpy() there is strcpy_s() that was added to C11, why not use them? Especially if they were added to the standard to prevent problems.

https://en.cppreference.com/w/c/string/byte/strcpy

MS may have "jumped the gun," but now it looks like the standard is catching up.
closed account (E0p9LyTq)
@MrCluckyToYou:

IMO there is more to updating older code than just removing deprecated/removed language features. There are changes to the language that "improve" how code is written. Sometimes in quite different ways.

Iterating through a std::vector, for instance:

Declare a vector:
std::vector<int> foo = { 1,2,3,4,5 };

An old way to print out all the elements:
1
2
3
4
5
   for (size_t x = 0; x < foo.size(); x++)
   {
      std::cout << ' ' << x;
   }
   std::cout << '\n';

std::vector has methods to get iterators, making for less error prone access to elements:
1
2
3
4
5
   for (std::vector<int>::iterator x = foo.begin(); x != foo.end(); x++)
   {
      std::cout << ' ' << *x;
   }
   std::cout << '\n';

C++11 added range-based for loops to the language making it easier to walk through a container:
1
2
3
4
5
   for (auto& x : foo) // access by reference
   {
      std::cout << ' ' << x;
   }
   std::cout << '\n';

Range-based for loops work with C-style arrays as well:
1
2
3
4
5
6
7
   int a[] { 0, 1, 2, 3, 4, 5 };
   // the initializer may be an array
   for (int n : a) // access by value (copies the std::vector)
   {
      std::cout << n << ' ';
   }
   std::cout << '\n';

New ways to do tasks that make for more compact, easier to read code.

Templates help make outputting a std::vector's elements more "intuitive."

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
#include <iostream>
#include <vector>
#include <string>

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

int main()
{
   std::vector<int> foo = { 1,2,3,4,5 };

   std::cout << foo << '\n';

   std::vector<std::string> sfoo = { "Hello", "World!" };

   std::cout << sfoo << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
{
   for (auto const& x : v)
   {
      std::cout << x << ' ';
   }

   return os;
}
Last edited on
closed account (E0p9LyTq)
@MrCluckyToYou:

Learning how to use the C++ <random> and <chrono> libraries wouldn't be a waste of time IMO. rand()/srand() have limitations.

https://wiki.sei.cmu.edu/confluence/display/c/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers
If there are C standard equivalent functions available, and with strcpy() there is strcpy_s() that was added to C11, why not use them?

It doesn't matter that there are "safe" functions available, the old functions are not deprecated. Those "safe" functions are optional and not all compilers support those functions.

@FurryGuy:

Thank you for taking the time to respond in depth like that. Honestly I'm getting a little overwhelmed and frustrated because well, I have hopes and dreams of doing this stuff for a living some day and it seems really far away for someone like me. But, I suppose the good news is that I feel like I'm understanding this stuff a lot better than I used to when I was younger. The hard part for me is finding something to DO with it on a daily basis so I can practice. You know, something tangible to create with the code so that I am constantly practicing and producing working examples so I feel like I'm actually accomplishing something. I'll definitely check out those libraries you mentioned though, and try to build their functionality into my programs going forward. Also, what would be the best source of documentation for the current standard and it's implementations?
closed account (E0p9LyTq)
The hard part for me is finding something to DO with it on a daily basis so I can practice.

Write a game or two, while learning a new C++ feature or way to code something.

There is lots of example code here at cplusplus in the reference section, compile and run a few of them so you can get a "feel" for what a language feature can do for you.

For some really in-depth high level examples look around at https://en.cppreference.com/w/

There are decent tutorials available here at cplusplus and https://www.learncpp.com/

Currently I am working on a C++ version of the old Star Trek game I originally played in BASIC. Also found a couple of C versions I use as reference.
Topic archived. No new replies allowed.
Pages: 12