Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板/命名空间交互_C++_Templates_Namespaces - Fatal编程技术网

C++ 模板/命名空间交互

C++ 模板/命名空间交互,c++,templates,namespaces,C++,Templates,Namespaces,我遇到一个朋友。。奇怪?最近,这让我相信,模板在创建时,是在与声明位置相同的名称空间中创建的(或者至少是使用相同的名称空间创建的)。就是, template<class T> class bar { public: static int stuff(){return T::stuff();} }; namespace ONE { struct foo { static int stuff(){return 1;} }; } name

我遇到一个朋友。。奇怪?最近,这让我相信,模板在创建时,是在与声明位置相同的名称空间中创建的(或者至少是使用相同的名称空间创建的)。就是,

template<class T>
class bar
{
public:
    static int stuff(){return T::stuff();}
};

namespace ONE
{
    struct foo
    {
        static int stuff(){return 1;}
    };
}

namespace TWO
{
    struct foo
    {
        static int stuff(){return 2;}
    };
}


using namespace TWO;

int main() 
{

    return bar<foo>::stuff();
}
模板
分类栏
{
公众:
静态int stuff(){return T::stuff();}
};
名称空间1
{
结构foo
{
静态int stuff(){return 1;}
};
}
名称空间二
{
结构foo
{
静态int stuff(){return 2;}
};
}
使用名称空间二;
int main()
{
返回条::stuff();
}
使用命名空间1时将返回1,使用命名空间2时将返回2

为什么??名称空间和模板之间是否存在其他“奇怪”或“意外”的交互


编辑:这在当时是令人困惑的,因为在多个文件中使用相同的模板,每个
使用不同的命名空间

这并不意外。您没有限定您想要的
foo
,因此您使用
声明告诉编译器在哪里可以找到它


我在生产代码中看到的最糟糕的模板抓到的是非依赖名称查找。这很复杂,所以最好只告诉你(第35.18-20节)。

我不知道这里有什么令人惊讶的地方。当您说
使用名称空间ONE
时,您将ONE::foo引入范围,现在被识别为
foo
。在上面的代码中,模板获取两个::foo作为其参数。它与模板无关,当您调用
bar::stuff()

时,在main()中发生的一切都在进行,这看起来一点也不“奇怪”<代码>使用命名空间1
相当于
bar::stuff()
,并按预期工作。我一定是缺少了什么?对我来说这似乎是正确的?酒吧就是酒吧,不是吗。那么这些数字似乎是对的?