模板化对象的编译顺序是什么?

Gra*_*Guy 2 c++ templates

抱歉找不到我问题的更好标题.基本上我注意到以下编译很好:

#include <vector>

void foo();

int main () {
    foo();
    return 0;
}

namespace{
struct point {
        double x, y;
    };
}

void foo(){       
    std::vector<point> p;
}
Run Code Online (Sandbox Code Playgroud)

而编译器抱怨以下内容:

#include <vector>

void foo();

int main () {
    foo();
    return 0;
}

void foo(){
    struct point {
        double x, y;
    };       
    std::vector<point> p;
}

// g++ output:
a.cpp: In function ‘void foo()’:
a.cpp:14: error: template argument for ‘template<class _Tp> class std::allocator’ uses local type ‘foo()::point’
a.cpp:14: error:   trying to instantiate ‘template<class _Tp> class std::allocator’
a.cpp:14: error: template argument 2 is invalid
a.cpp:14: error: invalid type in declaration before ‘;’ token
Run Code Online (Sandbox Code Playgroud)

我想知道第二种方法有什么问题?struct point在创建新std::vector<point>对象时是不是完全定义了?

R. *_*des 8

这是由于C++ 03中的限制(现在在C++ 11中提升),它只是禁止使用本地类型(即在函数体中定义的类型)作为模板参数.