Template specialization of a single method from a templated class

前端 未结 6 1546
北恋
北恋 2020-11-28 04:29

Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly:



        
6条回答
  •  温柔的废话
    2020-11-28 05:02

    If you want to remove the inline for whatever reason the solution of maxim1000 is perfectly valid.

    In your comment, though, it seems you believe that the inline keyword means that the function with all his contents gets always inlined but AFAIK that is actually very much dependent on your compiler optimization.

    Quoting from the C++ FAQ

    There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: the compiler might inline-expand some, all, or none of the places where you call a function designated as inline. (Don’t get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)

    So, unless you know that that function will actually bloat your executable or unless you want to remove it from the template definition header for other reasons, you can actually leave it where it is without any harm

提交回复
热议问题