#Reading types in C++
54 messages · Page 1 of 1 (latest)
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.
<type> <variable name>
<type> <variable name> = <value>
there exists BNF descriptions for the syntax of variable declarations
Yes, it's the same
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
Don't forget about the <type part 1> <variable name> <type part 2> style we know and love for functions and C arrays 🤡
nope, reference types are lost
template<typename T>
void f()
{
static_assert(sizeof(T) != sizeof(T), "");
}
#include <vector>
int main()
{
std::vector<int> v1;
using MysteryT = decltype(v1.begin());
f<MysteryT>();
}
the compile error reveals the type in the template function f
Is there a good source to learn about templates? I totally forgot about these lol
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
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; }
};
the global class Box takes in a template type T, and has a private field of type T called value
ez
so I understand for functions if you use a template it essentially just adjusts to the type
ye
you can do
funcionName<int,char,double>(1,'A',2.5);
just how it works?
the compiler copy pastes your type into T and prints a raw normal class
it's called
template instantiation
in the docs
so in the constructor if it's expecting a template then itll defual to do Box<Type> for example?
yes
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)
there's no assuming here
you must specify the template types when you create
Box<Something>
that is a special mechanism available for template functions called template deduction
but for classes if i have a constructor Box(T a); then I need to do Box<type> (...)
okay i see
and there's very insane template deduction guidelines for picking the most suitable candidate, detecting ambiguities too
are there any other cases I should know about with templates?
i understand classes and functions now
you can have
template<typename T1, typename T2, typename T3>
etc...
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
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
}
indeed. But this isn't to do with reading types but template programming
because <5> is more like its arguement
Yea sorry I steered away from the original topic
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?
when I give you a glass jug, can you drink water? nope, the jug is empty
when I give you a glass jug filled with water, or some carbonated drink, or fruit juice in there, can you drink it? yes
the glass jug are the template classes/functions, the liquid is the types that you place in there
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
C has the weak, unredable, horrific macros
C++ has the imporved type-safe readable templates
they both are used to do compile-time copy pasting
Okay I see