#Reading types in C++

54 messages · Page 1 of 1 (latest)

misty shell
#

I know that in C you go all the way right and then left to read types. Is this the same for c++? Or does it follow a different rule?

fiery epochBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

true shale
#

<type> <variable name> = <value>

#

there exists BNF descriptions for the syntax of variable declarations

stark raven
#

Ofc in both languages the parentheses can change this order

#

And in C++ there are more ambiguities, e.g. T x(y); can either be a function declaration or be mostly equivalent to T x = y;, depending on what y is

proud light
frail hamlet
frail hamlet
misty shell
#

Is there a good source to learn about templates? I totally forgot about these lol

#

Cppreference?

frail hamlet
# misty shell Cppreference?

ye, but there's like 5 different pages for templates,
it's messy, try with some code examples first to see the normal things like LinkedList<T>

#

then, move into advanced nightmares like .emplace, or enable_if

misty shell
#

It’s like you’re speaking for foreign language LOL. I remember templates slightly from my intro to C++ class but haven’t touched it since so idk anything about them now

#

Like I understand them for functions

#

But for something like this;

template <typename T>
class Box {
    T value;
public:
    Box(T v) : value(v) {}
    T getValue() { return value; }
};
frail hamlet
misty shell
#

so I understand for functions if you use a template it essentially just adjusts to the type

frail hamlet
#

ye

misty shell
#

but for classes it ends up being Box<type> ...

#

so how does it know to do this?

frail hamlet
misty shell
#

just how it works?

frail hamlet
#

it's called
template instantiation
in the docs

misty shell
#

so in the constructor if it's expecting a template then itll defual to do Box<Type> for example?

misty shell
#

why, if it can assume like functions?

#

is there a reason>

#

like if i had a function Square( T x); I can just do Square("Hi) or Square(1)

frail hamlet
frail hamlet
misty shell
#

but for classes if i have a constructor Box(T a); then I need to do Box<type> (...)

#

okay i see

frail hamlet
#

and there's very insane template deduction guidelines for picking the most suitable candidate, detecting ambiguities too

misty shell
#

are there any other cases I should know about with templates?

#

i understand classes and functions now

frail hamlet
#

and,

you can create
MyArray<T>

that has a templatified constructor that takes in iterators:

template<typename T>
template<typename Iter>
MyArray<T>::MyArray<T>(Iter beginIter, Iter endIter)
{

}
#

ez

misty shell
#

Apparently you can also do this?

#include <iostream>

template <int N>
void printNumbers() {
    for (int i = 0; i < N; ++i) {
        std::cout << i << " ";
    }
    std::cout << "\n";
}

int main() {
    printNumbers<5>();  // prints: 0 1 2 3 4
    printNumbers<10>(); // prints: 0 1 2 3 4 5 6 7 8 9
}
true shale
#

because <5> is more like its arguement

misty shell
#

I’m just confused on how this works

#

So templates can either replace values inside of things or adjust to different types? Based on how they’re used?

frail hamlet
misty shell
#

But I’m confused why sometimes templates are used to assume/work with any type and then another time the template just replaces values like macros

frail hamlet
#

they both are used to do compile-time copy pasting

misty shell
#

Okay I see