g ++不喜欢模板方法链模板var? [英] g++ doesn't like template method chaining on template var?
问题描述
我试图使用 g ++ 编译一些先前在 Visual C ++ 2008 Express Edition 下开发的代码,看起来g ++不会让我调用模板方法对模板变量的方法返回的引用。我可以把问题缩小到下面的代码:
class Inner
{
public:
template< typename T>
T get()const
{
return static_cast< T>(value_);
};
private:
int value_;
};
class Outer
{
public:
Inner const& get_inner(){return inner_; };
private:
inner inner_;
};
template< typename T>
int do_outer(T& val)
{
return val.get_inner()。get< int>();
}
int main()
{
外部;
do_outer(outer);
return 0;
}
代码在Microsoft的编译器下编译正常,但g ++引发错误: p>
$ g ++ -c main.cpp
main.cpp:在函数'int do_outer(T&)':
main.cpp:24:error:expected'int'之前的主表达式
main.cpp:24:error:expected';'before'int'
main.cpp:24:error:预期未限定ID之前'>'token
,其中第24行引用 return val.get_inner()。get< int>();
。
如果我使
do_outer
正常方法接收外部
引用代码编译。使 Inner :: get()
一个正常的方法也可以。并且使 Inner :: get()
return void并接收模板参数也是有效的,因为下面的 int 说明变得不必要,即: class Inner
{
public:
template< typename T>
void get(T& val)const
{
val = static_cast< T>(value_);
};
private:
int value_;
};
...
template< typename T>
int do_outer(T& val)
{
int i;
val.get_inner()。get(i);
return i;
}
...
遵守上面的代码。)
现在我的想法。有什么问题? gcc / g ++有问题吗?
我使用的编译器是:
$ g ++ --version
g ++(Ubuntu 4.3.3-5ubuntu4)4.3.3
<
模板< typename T>
int do_outer(T& val)
{
return val.get_inner()template get< int>();
}
我没有gcc atm的访问权限,问题和添加模板关键字总是解决他们。它也在VS中工作。
I'm trying to compile with g++ some code previously developed under Visual C++ 2008 Express Edition, and it looks like g++ won't let me call a template method on a reference returned by a method of a template variable. I was able to narrow the problem down to the following code:
class Inner
{
public:
template<typename T>
T get() const
{
return static_cast<T>(value_);
};
private:
int value_;
};
class Outer
{
public:
Inner const& get_inner() { return inner_; };
private:
Inner inner_;
};
template<typename T>
int do_outer(T& val)
{
return val.get_inner().get<int>();
}
int main()
{
Outer outer;
do_outer(outer);
return 0;
}
The code compiles fine under Microsoft's compiler, but g++ throws an error:
$ g++ -c main.cpp
main.cpp: In function ‘int do_outer(T&)’:
main.cpp:24: error: expected primary-expression before ‘int’
main.cpp:24: error: expected ‘;’ before ‘int’
main.cpp:24: error: expected unqualified-id before ‘>’ token
where line 24 refers to return val.get_inner().get<int>();
.
If I make do_outer
a normal method receiving an Outer
reference the code compiles. Making Inner::get()
a normal method also works. And making Inner::get()
return void and receive a template parameter also works because the int specifier below becomes needless, i.e.:
class Inner
{
public:
template<typename T>
void get(T& val) const
{
val = static_cast<T>(value_);
};
private:
int value_;
};
...
template<typename T>
int do_outer(T& val)
{
int i;
val.get_inner().get(i);
return i;
}
...
(g++ doesn't complaing about the code above.)
Now I'm out of ideas. What's the problem? Is there a problem with gcc/g++? Is there a compliance issue with my code?
The compiler I'm using is:
$ g++ --version
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
could you try with?
template<typename T>
int do_outer(T& val)
{
return val.get_inner().template get<int>();
}
I don't have access to gcc atm, but I've had similar issues and adding the template keyword always solved them. And it works in VS too.
这篇关于g ++不喜欢模板方法链模板var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!