Jump to content

c++ const, constexpr, macros

goatedpenguin

so as the title suggest can someone explain to me when the differences of const vs constexpr vs macros and when I should use each one of them. I would also appreciate if you could tell me what is the difference between runtime and compile time. Thanks in advance 🙂

 

Link to comment
Share on other sites

Link to post
Share on other sites

For const vs constexpr -> https://stackoverflow.com/questions/14116003/whats-the-difference-between-constexpr-and-const

A macro is basically something that the preprocessor replaces with code (before compiling your code)

For example

#define M_PI 3.1415

 

If you then write something like:

std::cout << M_PI << std::endl;

it would convert the code to:

std::cout << 3.1415 << std::endl;

and then compile it.

 

compile time vs runtime is also easy to understand.

 

Compile time is anything that can be known when the program is compiled. So basically if I write code like this:

int a = 1;

if(a)
{
	std::cout << "blub" << std::endl;
}
else
{
	std::cout << "blub but different" << std::endl;
}

the compiler will already know that a = 1 and thus won't even really need to incorporate the else into the code.

 

If you instead read a via an input into the program, a will only be defined at runtime.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, goatedpenguin said:

ic and what about macros?

sry, I accidentally hit strg enter, I put the rest into the edit, feel free to ask any questions you still have 🙂

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, adm0n said:

sry, I accidentally hit strg enter, I put the rest into the edit, feel free to ask any questions you still have 🙂

 is it true that in modern c++ ppl say to avoid macros cuz its not type safe, and its hard to debug ie. are there any use cases for macros compared to its counter parts like const and constexpr? also I have been learning c++ from this site: learncpp.com what are your thoughts on it? People say I should learn by doing but in a language like C++ not knowing features like polymorphism or OOP would defeat the purpose of the language right? like you might as well just code in C then(correct me if I am wrong tho)

Link to comment
Share on other sites

Link to post
Share on other sites

Well I wouldn't group macros in with const and const expressions. Macros are basically you programming the preprocessor, while const is just a flag that protects you and makes life easier on the compiler.

 

Macros still come from C, where you basically need them to get any amount of readability out of very complex code or at least make it adaptable. A lot of these use cases have been alleviated by C++ just providing the necessary tools.

 

The problem with macros is really that you can just confuse yourself into writing code that looks correct, but doesn't do what you want. And there is also a learning curve with macros. Consider the following

 

#define ADD(a, b) a + b
  
std::cout << ADD(1, 2) << std::endl;
// prints 3 as expected

std::cout << ADD(1, 2) * 3 << std::endl; 
// prints 7, but correct would be 9
// the code gets completed to std::cout << 1 + 2 * 3 << std::endl; 

 

if you set something const, it will really just throw errors when you accidentally change it in some function.

Link to comment
Share on other sites

Link to post
Share on other sites

52 minutes ago, goatedpenguin said:

People say I should learn by doing but in a language like C++ not knowing features like polymorphism or OOP would defeat the purpose of the language right? like you might as well just code in C then(correct me if I am wrong tho)

You don't need to use polymorphism or any other fancy OOP features when using C++. In fact, you could write C++ code just like how you would write in C and have it work just the same.

 

Why might someone prefer to write in C++ instead of C, if they aren't making use of OOP? For one, C++ has the standard template library. It's a set of generic data structures and algorithms, among other things, that I find to be very useful. C++ also has additions to help manage memory, like using smart pointers instead of raw pointers.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, dcgreen2k said:

You don't need to use polymorphism or any other fancy OOP features when using C++. In fact, you could write C++ code just like how you would write in C and have it work just the same.

 

Why might someone prefer to write in C++ instead of C, if they aren't making use of OOP? For one, C++ has the standard template library. It's a set of generic data structures and algorithms, among other things, that I find to be very useful. C++ also has additions to help manage memory, like using smart pointers instead of raw pointers.

I get that not using fancy features is not always the needed but my point is that how would one come to know that when they are learning to code by doing and not reading a book that smart pointers exist or the template lib exists or the c++ standard lib is there. For an example if you tell me to make a simple banking system which lets a user withdraw money, encrypt there passwords, merge account etc, I would not know where to begin and what I should be using or doing cuz making a banking system program in c++ is very different from making a banking system in python or ruby. Like what is the proper way to learn c++ cuz I feel like when I am learning a new topic in c++ I sometimes think "how tf am I suppose to remember all of this and the nuances?"

Link to comment
Share on other sites

Link to post
Share on other sites

42 minutes ago, goatedpenguin said:

I get that not using fancy features is not always the needed but my point is that how would one come to know that when they are learning to code by doing and not reading a book that smart pointers exist or the template lib exists or the c++ standard lib is there. For an example if you tell me to make a simple banking system which lets a user withdraw money, encrypt there passwords, merge account etc, I would not know where to begin and what I should be using or doing cuz making a banking system program in c++ is very different from making a banking system in python or ruby. Like what is the proper way to learn c++ cuz I feel like when I am learning a new topic in c++ I sometimes think "how tf am I suppose to remember all of this and the nuances?"

If you already know hot to code in theory, then you learn by doing (i.e. think of a small project and try to use multiple ways to achieve the same thing. At the end you can judge yourself what you liked more), or by reviewing other peoples code. Ideally code, that has already gone through review process from other people.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, adm0n said:

If you already know hot to code in theory, then you learn by doing (i.e. think of a small project and try to use multiple ways to achieve the same thing. At the end you can judge yourself what you liked more), or by reviewing other peoples code. Ideally code, that has already gone through review process from other people.

So your advice is i should learn by doing?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, goatedpenguin said:

I get that not using fancy features is not always the needed but my point is that how would one come to know that when they are learning to code by doing and not reading a book that smart pointers exist or the template lib exists or the c++ standard lib is there. 

In my experience, it comes from when you're trying to solve a problem and you get stuck. Or, you've already solved it and want to optimize your code.

 

To fix that, you Google a question related to the problem you're having and see what other people have done. Simple as that. At some point you'll come across solutions that use smart pointers, the template lib, etc, and try them out for yourself.

 

Learning to code by doing doesn't just mean sitting down and writing code, because it's impossible to get anywhere by just doing that. You need external resources to learn from to get better and that doesn't have to be from a textbook.

 

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, goatedpenguin said:

So your advice is i should learn by doing?

Not necessarily. You need to code to actually learn it, but that much is clear to you anyway. Understanding what is written in a textbook, some tutorial or a stack overflow answer is also important. But you will never actually memorize all the things you can learn, if you don't actively use them a lot. And there will be things you'll learn and forget again, since you aren't using them enough.

 

There are also things that are good to know, but you never know them. A good example would be function decorators in python. They make the code so much nicer, but you will never actually code something and notice, that you'll need decorators if you have never heard about them. So learning and discovering is also important. That's why looking at other peoples code or having other people look at your code is important too!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×