Candidate template ignored because template argument could not be inferred

后端 未结 3 1678
执念已碎
执念已碎 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:42

    Firstly, using :: for referencing a nested struct is not correct. The right way is to use A.x, where x is a member of type X. But for this to work, you need to declare two members x, y of type X and Y, respectively.

    Secondly, I think that a good practice is to separate the template declarations of struct X and struct Y from the declaration of struct A in order to avoid the nested struct declaration.

    In short, I would rewrite your code as followings:

    template
    struct X {
        K p;
    };
    
    template
    struct Y {
        K q;
    };
    
    template
    struct A {
        X x;
        Y y;
    };
    
    template
    void foo(const X& x, const Y& y) {
        std::cout << "A" << std::endl;
    }
    
    int main() {
        A a;
        foo(a.x, a.y);
        return 0;
    }
    

提交回复
热议问题