Why the fuck do I have to repeat "template " and before the class name on every function definition?
Hunter Nelson
I dunno, I only program in C. If I want generic types and data hiding, I use macros and pointers to opaque static types.
Aiden Reyes
Just put all the logic in the class definition to not use shit::. Anyways the compiler can't know if you want to use that template of the function or a new function with no template since you can have both at the same time (and more with template specialization).
Carter Young
I have never used a template in my life.
Lucas Scott
same here, even though I understand its sometimes useful
Luis Foster
Well the straigjt awnser is to give it proper class scope.
My opinionated awnser is that its because c++ is shit.
Just program everything in html instead! XDDDDDDD
Sebastian Allen
>t. Go dev
Jonathan Johnson
You only need to do that if you're providing template specializations for particular things, like if you want to make foo have its own particular definition instead of a generic one. For regular generic template definitions, define them all in the header file. You can't split them into a separate compilation unit (cpp file) like the example you wrote anyway, as the compiler needs to compile new specializations of the template as they're needed and so it needs the source of that definition included in the compilation unit, so just inline them in the class; e.g.
template class foo { void bar(const T &t) { // ... } };
Christopher Rogers
i wish you could do something like this without wrapping into a class template { void foo(T t) {} void bar(T t) {} }
Robert Wilson
It's not shitty, you're just dumb.
Colton Hernandez
>making everything take ten times as long so it's more human readable fast CPUs were a mistake
Jeremiah Ortiz
you're just doing it wrong
Charles Sullivan
What's even going on behind the scenes with template compilation?
The x86 just shows the calls with the appropriate type.
Owen Gray
>Why the fuck do I have to repeat "template " and before the class name on every function definition?
How would you otherwise specify that the type template parameter of foo is the same as the const-reffed type of the first argument of bar?
There might be other, overloaded methods named bar inside foo, und you need a way to fully specify which one you mean.
Isaac Thomas
The compiler just compiles each instantiation.
Additionally it then might merge functionally same code using COMDAT folding.
Camden Kelly
You can, just repeat template each function and leave the functions in some namespace.
Dylan Ortiz
Does it just suck being you?
Easton Sanchez
You are missing out on truly higher planes of existence
Jason Rodriguez
Every "weird" and "stupid" thing in C++ has a very good reason for being how it is.
Owen Thompson
The standards committee pedalled back a couple of times in the past. See auto_ptr, trigraphs, "typename" in template template parameters, making constexpr actually useful, fixing noexcept, etc.
They are only humans, and need some time to see what the actual impact of language features is and how they all interact. However, they collectively probably put more thought into the standard than one programmer can in his/her lifetime.
Luke Mitchell
Because I don't want my static analyzer to through a hissyfit unko.
James Bennett
You are a literal retard, templates do not work like that. The definitions need to be known at compile time, putting them in separate translations units do not work. Put those definitions in the class definition.
Connor Sanchez
Uhhh what? C++ is just as fast, and sometimes faster than C. Templates allow some really nifty optimization tricks. For example, the destructor for std::vector doesn't call a destructor for each int since it's POD (it gets optimized out easily). But the destructor for std::vector for a T that has a destructor, calls the destructor for each T.
In Java for example, because of type erasure it can't do those optimizations.
Josiah Miller
>sometimes faster than C
For example?
Michael King
This is the most shallow complaint in an ocean of flaws. Go watch a Scott Meyers talk or something. Then do some basic performance work on code. There, now you're properly jaded. Way easier to reach this place than with other languages. >it's sometimes even faster than C >we have all these cool optimizations >we don't destruct ints in a vector user do you not see how this was a problem C++ introduced and then solved? That you even considered this a gain over C shows how brain damaged you are. It's not even your fault. C++ does this. It's a language that proposes the same low level as C yet the actual developers put their minds in a completely different place.
Even stranger to me is that you've written this when there's the glaring issue of destructor being defined only for single classes and not groupings like in the case of vectors. That's where you could actually make C++ neat and intelligent on this front. But to do this implies replacing every structure you'd wish to store your objects in to enable this behavior. Which in turn is more like having list destroyer (not destructor). Which were trivial to do in the first place. Anywhere really. If you didn't get that it's a problem with the destructor/constructor model and RAII being aimed at a very slim set of cases. Heresy I know.
If we weren't taking about C++ I'd call you out for false flagging. Normal people don't think like this.
Jeremiah Parker
the compiler creates a new class each time you use it with a different class type
so if you have a vector, vector, vector, the compiler will build 3 separate vector classes each handling their own designated type
Logan Ramirez
And I should probably not be so biased in my posts. The upside to C++ is that its oriented towards allowing a small part of the team work on performance while the rest of the team can work on functionality. It's difficult to manage this. It requires very strong communication and tough discussions on API choices. But for big companies it's gonna be worth it.
Aiden Taylor
std::sort vs qsort.
Levi Rogers
don't use templates
Nicholas Turner
I just wrote a GUI graph widget for my game, the data it generates the visual graph from might could be a float, int, unsigned int, etc.
Instead of writing my graph widget multiple times for each value type, I add template and the compiler will generate variants of that class as needed.
Isaiah Torres
also templates are handy for avoiding virtual classes and the overhead that comes with that.
Gabriel Sanders
I never understood virtual methods Do they just override inheritance?
I've never even seen virtual classes
Zachary Nguyen
I've thought about how much more abstract one can make c++ be. If you want it to be easier, create a script to auto complete templates, for loops, etc
Or do python. Coming from c++ python feels like a 4th level programming language
William Jackson
You don't want to see it. I've seen hell. Be glad you haven't seen it too.
Ryder Kelly
>Do they just override inheritance? Yeah, it's like inheritance where you have different methods but shared names, also having pointers/vectors of generic objects. But it's costly run-time polymorphism involving a lookup table.
Alternatively passing non-virtual classes to a template function with common method names will generate different functions at compile-time, avoiding the whole indirect lookup table thing.
Angel Lopez
Mmkay I've done some more research on it and it appears virtual classes are to prohibit multiple instances of a class such as calling A::Foo() from class D with this inheritance structure A / \ B C \ / D
And the methods use virtual when an abstract Base class is needed... Why must there be a virtual method in the base class if all other subclasses define their own methods?
Such as here: #include
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
// This class ingerits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout
Adrian Long
OP here, if I shouldn't specify the template on the class.
Then how can I specify the type on the constructor?
Bentley Nguyen
In your example it is not necessary. But the idea is that Base is the interface. If you have multiple classes deriving, you can have different implemenations of "fun". Any code that works with a Base pointer or reference will be able to call the speciallized "fun" in any subclass without knowing about the subclass.
Colton Hall
Another rust thread
Brayden Jackson
>Why was it necessary to define a virtual method (that's not used) in the base class when the subclass defines it for itself? doing that forces subclasses to implement it (as opposed to using an empty { } stub which would make implementing it optional)
Cooper Reyes
Thanks, it's starting to make sense... How does it know which method to execute if it doesn't know the subclass though?
Hunter Bailey
>splitting template classes in .h and .cpp files Rustbabbies are literally retarded.
Cameron Phillips
Templates only compile when you instantiate them. You can test this by writing something retarded in a template and hitting compile without intantiating that template. You won't get a compiler error.
Noah Martinez
Are you saying it's just to remind the programmer to implement the method for each subclass?
Hudson Robinson
imagine doThing is actually a complicated series of operations. (reposted to remove unneed memory include) #include
// Virtual Example class VirtClass { public: virtual int virtMethod(const int _int) const = 0; // = 0 makes this a "pure" virtual method }; class VirtClassA: public VirtClass { public: virtual int virtMethod(const int _int) const override { // override is redundant because it's pure but whatever return _int / 5; // COMPLEX MATH } }; class VirtClassB: public VirtClass { public: virtual int virtMethod(const int _int) const override { // override is redundant because it's pure but whatever return _int * 5; // TOTALLY DIFFERENT MATH } }; void doVirtThing(const VirtClass& _virtClass) { std::cout
Ayden Powell
You can also pass virt_a/virt_b to doThing since the template will just make a new function for the virtual class (you'll till need a lookup). You cannot pass a/b to doVirtThing because they are not part of the VirtClass. Virtual classes can be useful for polymorphic typed pointers, but you could alternatively make a template wrapper class for your pointer.
Christian Butler
OP here.
How can I add a template to the constructor?
Aiden Rivera
>const int _int pajeet desu
Jaxon Hill
pretty much, though it's less a friendly reminder and more beating you with a stick if you don't satisfy the requirements of the interface as defined in the base class
i don't personally use it much but then we're not hardcore oop types at my work, we tend to use classes for encapsulation only and follow a more functional style
thinking up names for shit has to be the most time-consuming things we do as programmers, reeee