Candidate template ignored because template argument could not be inferred

后端 未结 3 1665
执念已碎
执念已碎 2020-12-09 14:53

What is wrong with the following piece of code?

#include 

template
struct A {
    struct X { K p; };
    struct Y { K q; }         


        
3条回答
  •  醉梦人生
    2020-12-09 15:35

    The argument K in const typename A::X is not deducible. Basically, everything left of a :: is not deducible (if :: separates a nested-name).

    It's trivial to see why it makes no sense to ask for deduction by running through this thought experiment:

    struct A { typedef int type; }
    struct B { typedef int type; }
    
    template  void foo(typename T::type);
    
    foo(5);   // is T == A or T == B ??
    

    There's no one-to-one mapping from types to nested types: Given any type (such as int), there could be many ambient types of which it is a nested type, or there needn't be any.

提交回复
热议问题