C++模板类实例问题

Nee*_*n K 0 c++ templates

#include <iostream>
#include <string>

using namespace std;

template<class T> class Sample {
private:
 T val;
public:
 Sample(T InitialVal=T()) : val(InitialVal)
 {
    // do nothing
 }
 ~Sample() 
 {
    // do nothing
 }
 void PrintVal(void)
 {
     try {
    cout << "[" << val << "]" << endl;
     } catch(...) {
        cout << "exception thrown" << endl;
     }
 }
};

int main() {
    // your code goes here
    Sample<int> ints(20), intd;
    Sample<char *> chars(const_cast<char*>("Neelakantan")), charsd;
    Sample<string> s, ss("neel");

    ints.PrintVal();
    intd.PrintVal();

    chars.PrintVal();
    charsd.PrintVal(); // <<- Culprit line. Commenting out this line works as expected.

    s.PrintVal();
    ss.PrintVal();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,我得到以下输出:

sh-4.4$ g++ -o main *.cpp                                                                                                                                                       
sh-4.4$ main                                                                                                                                                                    
[20]                                                                                                                                                                            
[0]                                                                                                                                                                             
[Neelakantan]                                                                                                                                                                   
[sh-4.4$
Run Code Online (Sandbox Code Playgroud)

当我注释掉" charsd.PrintVal();" 这一行时,我得到以下输出:

[sh-4.4$ g++ -o main *.cpp                                                                                                                                                      
sh-4.4$ main                                                                                                                                                                    
[20]                                                                                                                                                                            
[0]                                                                                                                                                                             
[Neelakantan]                                                                                                                                                                   
[]                                                                                                                                                                              
[neel]                                                                                                                                                                          
sh-4.4$
Run Code Online (Sandbox Code Playgroud)

Sample <char*>类型的模板实例的'charsd'对象有什么问题?没有例外被抛出.

编译器版本:

sh-4.4$ g++ --version                                                                                                                                                           
g++ (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)                                                                                                                                      
Copyright (C) 2017 Free Software Foundation, Inc.                                                                                                                               
This is free software; see the source for copying conditions.  There is NO                                                                                                      
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                                                                                                     

sh-4.4$
Run Code Online (Sandbox Code Playgroud)

use*_*670 5

问题是,现场val;charsd对象与空指针初始化.因此,尝试将其传递给operator <<违反运算符前置条件并导致未定义的行为.