How does Stroustrup's Can_Copy template work?

后端 未结 4 689
野趣味
野趣味 2020-12-19 08:49

Stroustrup provides a Can_copy template. How does it work?

template struct Can_copy {
    static void constraints(T1 a, T2 b) { T2          


        
4条回答
  •  别那么骄傲
    2020-12-19 09:08

    It's because not used member functions in templates don't exist in generated code, so to check constrainst you would have to call constraints() explicitly somewhere.

    This way code for constraints() is generated, so constraints are checked in compile time (and that is the purpose of Can_copy).

提交回复
热议问题