相关疑难解决方法(0)

g ++/Clang中的另一个错误?[C++模板很有趣]

查看以下代码(仅为了好玩而编写)

namespace N
{
   template<typename T>
   struct K
   {

   };
}
template<typename T>
struct X
{
   typename T::template K<T> *p; //should give error 
                                 //N::K<int> has no template member named `K`
};

int main()
{
   X<N::K<int> > l;
}
Run Code Online (Sandbox Code Playgroud)

代码在g ++(4.5.1)和Clang上编译,而Comeau和Intel C++给出(类似)错误.

我在Comeau上遇到的错误是:

"ComeauTest.c", line 13: error: class "N::K<int>" has no member "K"
     typename T::template K<T> *p;
                          ^
          detected during instantiation of class "X<T> [with T=N::K<int>]" at
                    line 18

"ComeauTest.c", line 13: error: expected an identifier
     typename T::template K<T> *p;
                           ^ …
Run Code Online (Sandbox Code Playgroud)

c++ templates

31
推荐指数
1
解决办法
3442
查看次数

"结束"不能用于模板功能

我想在命令startend函数模板中使用一个带有成员变量的简单结构:

#include <iostream>
using namespace std;

struct st {
    int start;
    int end;
};

template<typename T>
void compare(const T& v1, const T& v2){
    if(v1.end < v2.end)
        cout << "v1 < v2" << endl;
}

int main() {
    st a = {1, 2};
    st b = {2, 3};
    compare(a, b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是这个程序无法在mingw g ++ 4.8.2上编译:

main.cpp: In function 'void compare(const T&, const T&)':
main.cpp:11:11: error: parse error in template argument list
     if(v1.end < v2.end)
           ^ …
Run Code Online (Sandbox Code Playgroud)

c++ templates

9
推荐指数
2
解决办法
325
查看次数

标签 统计

c++ ×2

templates ×2