Is there an easy way to find out if a class instance was an instance in a translation block? An exercise from C ++ Primer asks each tagged statement whether an instance occurs:
template <typename T> class Stack { };
void f1(Stack<char>);
class Exercise {
Stack<double> &rsd;
Stack<int> si;
};
int main() {
Stack<char> *sc;
f1(*sc);
int iObj = sizeof(Stack< string >);
}
I am not sure how I could check my answers. I thought that perhaps I could use explicit instances for each type of class (for example extern template class Stack<char>
), and then never have a corresponding explicit definition of instantiation in a program. Thus, if something was created, if the definition did not appear later, the linker will raise an error.
However, the compiler / linker does not always recognize this error:
template <typename T> class A{ };
extern template class A<int>;
int main(){
A<int> a;
}
gcc 4.9.2. , , , [14.7.2] [11] N3337:
, , . , , (14.7.1) , - ; , .
( , " ", ?). , , , - , Stack
?
template <typename T> class Stack;
, , ?