g ++编译错误的另一件事情在msvs工作 [英] g++ compilation error of another thing that works in msvs

查看:147
本文介绍了g ++编译错误的另一件事情在msvs工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heya,伙计。我正在移植一些从MSVS开发的项目中使用g ++的代码。我发现了很多小的差异,主要是MSVS允许的,但g ++不。通常它是涉及c ++标准的东西,MSVS允许的幻灯片,但我遇到麻烦看到一个特定部分有什么问题。



g ++有麻烦匹配呼叫to operator!=,但只在特定上下文中。查找运算符!=如果托管类不是模板,则特定嵌套类可用。如果我把主机类转换为类模板,但是,一切都打破了。我要么缺少一些基本的c ++,或g ++做错了。



我学会了不要哭编译器错误!太多了,所以我想看看有没有人能看到我错过了什么。



这个工作示例显示工作,非模板版本,然后破碎,模板版本。
g ++ --version:g ++(Ubuntu 4.4.1-4ubuntu9)4.4.1



不带模板的工作参考版本 / p>

  namespace Works {

struct host {
struct iterator {};
iterator op();
};

bool operator!=(host :: iterator const& a0,host :: iterator const& a1);

bool f(){
return host()。op()!= host()。op();
}

} //命名空间作品

包含模板的破损版本

 命名空间Broken {

template< typename T&
struct host {
struct iterator {};
iterator op();
};

template< typename T>
bool operator!=(typename host< T> :: iterator const& a0,
typename host< T> :: iterator const& a1);

bool f(){
return host< int>()。op()!= host< int>()。
}

} //命名空间Broken

失败并显示错误:

  Main.cpp:在函数'bool Broken :: f()':
Main .cpp:50:error:没有匹配'operator!='in'Broken :: host< int>()。Broken :: host< T> :: op [with T = int] host< int>()。Broken :: host< T> :: op [with T = int]()'


问题是在 host< ; T> :: iterator T 在不可推演的上下文中。由于两个参数都不允许推导 T ,所以不能实例化函数模板。



这就是为什么你通常定义重载

  struct iterator 
{
friend bool operator!=(iterator const& lhs,iterator const& rhs)
{
return false;
}
};


Heya, folks. I'm porting some code from a project largely developed in MSVS to use g++. I've found a lot of little differences, mostly things that MSVS allows but g++ does not. Usually it's something involving c++ standards, things that MSVS lets slide, but I'm having trouble seeing just what's wrong with one particular section.

g++ is having trouble matching a call to operator !=, but only in a specific context. Looking up operator != for a particular nested class works if the hosting class is not a template. If I turn the hosting class into a class template, however, everything breaks. I'm either missing something fundamental to c++, or g++ is doing something wrong.

I've learned not to cry "Compiler Bug!" too often, so I wanted to see if anyone here can see what I'm missing.

This worked example shows the working, non-template version, and then the broken, template version. g++ --version gives: g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1

Working reference version without templates

namespace Works {

struct host {
    struct iterator {};
    iterator op();
};

bool operator != (host::iterator const& a0, host::iterator const& a1);

bool f() {
    return host().op() != host().op();
}

} // namespace Works

Broken version with templates

namespace Broken {

template <typename T>
struct host {
    struct iterator {};
    iterator op();
};

template <typename T>
bool operator != (typename host<T>::iterator const& a0, 
                   typename host<T>::iterator const& a1);

bool f() {
    return host<int>().op() != host<int>().op();
}

} // namespace Broken

The template version fails with the errors:

Main.cpp: In function ‘bool Broken::f()’:
Main.cpp:50: error: no match for ‘operator!=’ in ‘Broken::host<int>().Broken::host<T>::op [with T = int]() != Broken::host<int>().Broken::host<T>::op [with T = int]()’

解决方案

This works neither in msvc nor gcc.

The problem is that in host<T>::iterator, T is in non-deducible context. Since neither parameter allows T to be deduced, the function template cannot be instantiated.

That's why you usually define overloaded operators inside the class.

struct iterator
{
    friend bool operator != (iterator const & lhs, iterator const & rhs)
    {
        return false;
    }
};

这篇关于g ++编译错误的另一件事情在msvs工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆