When can a template method use a function defined later, without forward declaration?

后端 未结 1 1014
鱼传尺愫
鱼传尺愫 2021-01-27 08:53

I am writing a simple logger class similar to QDebug, which has a template method that save data into QStringList. The code is here:

#include 

        
相关标签:
1条回答
  • 2021-01-27 09:05

    The name log is looked up twice. At the point of template definition, ordinary lookup is performed. It doesn't find anything, since log is not declared at this point.

    Then, at the point of instantiation, only argument-dependent lookup is performed. When the parameter is of type QString, the global namespace is searched since QString is declared there, and so log(QString) is found. But the type int[] doesn't have any associated namespaces, so argument-dependent lookup has nothing to search and finds nothing. Hence the error.

    0 讨论(0)
提交回复
热议问题