使用模板的方法嵌套结构

Meg*_*les 12 c++ struct

我想将python嵌套的defs翻译成c ++,我正在使用嵌套的结构.

#include<iostream>
static void ffunc(int x, int y)
{
    static int x1 = x;
    static int y1 = y;
    struct g
    {
        static void gfunc(int z)
        {
            static int z1 = z;
            std::cout << x1 << y1 << z << std::endl;

            struct h
            {
                static void hfunc(int k)
                {
                    std::cout<< x1 << y1 << z1 << k <<  std::endl;
                }
            };
            h::hfunc(4);
        }
    };
    g::gfunc(3);
}
int main()
{
    ffunc(1, 2);
}
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常.问题是,我想要模板,以便嵌套函数可以使用任何类型参数.但是,当我尝试使用模板时:

#include<iostream>
template <typename t1>
static void ffunc(t1 x, t1 y)
{
    static t1 x1 = x;
    static t1 y1 = y;
    struct g
    {
        static void gfunc(int z)
        {
            static int z1 = z;
            std::cout << x1 << y1 << z << std::endl;

            struct h
            {
                static void hfunc(int k)
                {
                    std::cout<< x1 << y1 << z1 << k <<  std::endl;
                }
            };
            h::hfunc(4);
        }
    };
    g::gfunc(3);
}
int main()
{
    ffunc(1, 2);
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

/tmp/cceIMovo.o: In function `void ffunc<int>(int, int)::g::gfunc(int)':
nested.cc:(.text+0x17d): undefined reference to `y1'
nested.cc:(.text+0x183): undefined reference to `x1'
/tmp/cceIMovo.o: In function `void ffunc<int>(int, int)::g::gfunc(int)::h::hfunc(int)':
nested.cc:(.text+0x1e5): undefined reference to `z1'
nested.cc:(.text+0x1ec): undefined reference to `y1'
nested.cc:(.text+0x1f2): undefined reference to `x1'
Run Code Online (Sandbox Code Playgroud)

有谁知道我想做什么是可能的c ++?谢谢!

Nik*_* C. 2

我可能是错的,但这看起来像是一个编译器错误。GCC (4.7.2) 会产生该错误,而 Clang (3.1) 和 ICC (13.0.0) 则可以很好地处理它。

尝试在其他编译器上重现此内容的最少代码:http://pastebin.com/kf2sF3NL