It\'s an exercise from C++ Primer 5th Edition:
Exercise 16.27: For each labeled statement explain what, if any, instantiations happen. If a tem
In your specific case a declaration doesn't mean an instantiation
#include
using namespace std;
template class Stack {
typedef typename T::ThisDoesntExist StaticAssert; // T::NotExisting doesn't exist at all!
};
void f1(Stack); // No instantiation, compiles
class Exercise {
Stack &rsd; // No instantiation, compiles (references don't need instantiation, are similar to pointers in this)
Stack si; // Instantiation! Doesn't compile!!
};
int main(){
Stack *sc; // No Instantiation, this compiles successfully since a pointer doesn't need instantiation
f1(*sc); // Instantiation of Stack! Doesn't compile!!
int iObj = sizeof(Stack< std::string >); // Instantiation of Stack, doesn't compile!!
}
notice the pointer/reference stuff: they don't require instantiation since no data is actually allocated (a pointer is just a few bytes to contain the address, has no need to have all the data stored.. take a look at the pimpl idiom ).
Only when stuff is allocated then the template has to be completely resolved (and that happens at compile-time, that's why they usually need both declaration and definition.. there's no linking phase yet)