Checking if there was an instance of a class template?

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>); // (a)
class Exercise {
    Stack<double> &rsd; // (b)
    Stack<int> si; // (c)
};
int main() {
    Stack<char> *sc; // (d)
    f1(*sc); // (e)
    int iObj = sizeof(Stack< string >); // (f)
}

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;

, , ?

+4
1

nm . , . gcc .

"-fdata-sections", "-ffunction-sections" , ( ) , . "-gc-sections" .

0

Source: https://habr.com/ru/post/1613976/


All Articles