逗号超载语义 [英] comma overload semantics

查看:85
本文介绍了逗号超载语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下编译除非第一行被取消注释。

当我尝试取消注释第一行时:


错误:''B& FM :: operator,(FM :: mystruct< A>,B&)'':无法推断出''FM :: mystruct<
模板参数; A>''来自

''std :: basic_string< _Elem,_Traits,_Ax> :: _ Myt''

with

[

_Elem = char,

_Traits = std :: char_traits< char>,

_Ax = std :: allocator< char>

]


只要''mystruct''是一个普通的struct就不会出现错误,但是有一些

模板版有些麻烦。


错误指的是''assign''方法

''std :: basic_string< _Elem,_Traits,_Ax>'':


_Myt& assign(const _Myt& _Right,size_type _Roff,size_type _Count)

{// assign _Right [_Roff,_Roff + _Count)

... //这里的一些代码

if(this ==& _Right)

erase((size_type)(_ Roff + _Num)),erase(0,_ Roff); // substring

... //这里的一些代码

}


我很困惑因为''mystruct''没有声明任何ctors并且通常

编译器不应该尝试向它投射任何东西。因此,在失败后找到适当的重载,必须使用默认版本。

我的编译器是否有任何问题,或者我必须声明默认值

版本?我的意思是这样的事情:


模板< typename A,typename B>

inline B& operator,(A&,B& b){return b;}; //全局默认逗号

命名空间


对重载是否有任何限制逗号?

重载逗号有什么语义?


// #define uncomment

#include< iostream>


#ifdef取消注释/ *模板版本的mystruct * /

#define BiTemplate(A,B)模板< typename A,typename B>

#define UnoTemplate(A)template< typename A>

#define with(A)< A>

#else / *无模板版本的mystruct:忽略A。* /

#define BiTemplate(A,B)模板< typename B>

#define UnoTemplate(A)

#define with(A)

#endif

命名空间FM {


UnoTemplate(A)

struct mystruct {

};


BiTemplate(A,B)

B& operator,(mystruct With(A),B& i){return i;};


};

使用命名空间FM;


void main(void){}; //什么都不做


提前致谢,

FM。


the following compiles unless the first line is uncommented .
when I try to uncomment the first line I get:

error : ''B &FM::operator ,(FM::mystruct<A>,B &)'' : could not deduce
template argument for ''FM::mystruct<A>'' from
''std::basic_string<_Elem,_Traits,_Ax>::_Myt''
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

As long as ''mystruct'' is a normal struct no error occures,but there is
some trouble with template version.

error refers to to the ''assign'' method of
''std::basic_string<_Elem,_Traits,_Ax>'' :

_Myt& assign(const _Myt& _Right, size_type _Roff, size_type _Count)
{ // assign _Right [_Roff, _Roff + _Count)
...//some code here
if (this == &_Right)
erase((size_type)(_Roff + _Num)), erase(0, _Roff);// substring
...//some code here
}

I am confused since ''mystruct'' does not declare any ctors and normally
the compiler should not try to cast anything to it . So, after failure
to find the appropriate overload , the default version must be used .
Is there any problem with my compiler or I have to declare a default
version? I mean some thing like this:

template <typename A, typename B>
inline B& operator,(A &,B& b){return b;};//default comma in global
namespace

Is there any restriction on overloading comma?
What are the semantics for overloading comma?

//#define uncomment
#include <iostream>

#ifdef uncomment/*template version of mystruct*/
# define BiTemplate(A,B) template < typename A ,typename B >
# define UnoTemplate(A) template < typename A >
# define With(A) < A >
#else/*none template version of mystruct: ignore A .*/
# define BiTemplate(A,B) template < typename B >
# define UnoTemplate(A)
# define With(A)
#endif
namespace FM{

UnoTemplate(A)
struct mystruct{
};

BiTemplate(A,B)
B& operator,(mystruct With(A) ,B& i){return i;};

};
using namespace FM;

void main(void){};//just do nothing

Thanks in advance,
FM.

推荐答案

terminator写道:
terminator wrote:

以下编译除非第一行被取消注释。

当我尝试取消注释我得到的第一行:
the following compiles unless the first line is uncommented .
when I try to uncomment the first line I get:



代码在这里编译未注释或评论

使用VC8和icc 9.1

the code compiles here either uncommented or commented

using VC8 and icc 9.1


On 4 Srp,13:54,terminator< farid.mehr ... @ gmail.comwrote:
On 4 Srp, 13:54, terminator <farid.mehr...@gmail.comwrote:

除非第一行被取消注释,否则以下编译。

当我尝试取消注释第一行时:


错误:''B& FM :: operator,(FM :: mystruct< A>,B&)'':无法推断出''FM $'
模板参数:: mystruct< A>''来自

''std :: basic_string< _Elem,_Traits,_Ax> :: _ Myt''

with

[

_Elem = char,

_Traits = std :: char_traits< char>,

_Ax = std :: allocator< char>

]


只要''mystruct''是一个普通的struct,就不会出现错误,但是有一些麻烦模板版本。


错误指的是''分配''方法

''std :: basic_string< _Elem,_Traits,_Ax>' ':


_Myt& assign(const _Myt& _Right,size_type _Roff,size_type _Count)

{// assign _Right [_Roff,_Roff + _Count)

... //这里的一些代码

if(this ==& _Right)

erase((size_type)(_ Roff + _Num)),erase(0,_ Roff); // substring

... //这里的一些代码

}


我很困惑因为''mystruct''没有声明任何ctors并且通常

编译器不应该尝试向它投射任何东西。因此,在失败后找到适当的重载,必须使用默认版本。

我的编译器是否有任何问题,或者我必须声明默认值

版本?我的意思是这样的事情:


模板< typename A,typename B>

inline B& operator,(A&,B& b){return b;}; //全局默认逗号

命名空间


对重载是否有任何限制逗号?

重载逗号有什么语义?


// #define uncomment

#include< iostream>


#ifdef取消注释/ *模板版本的mystruct * /

#define BiTemplate(A,B)模板< typename A,typename B>

#define UnoTemplate(A)template< typename A>

#define with(A)< A>

#else / *无模板版本的mystruct:忽略A。* /

#define BiTemplate(A,B)模板< typename B>

#define UnoTemplate(A)

#define with(A)

#endif


命名空间FM {


UnoTemplate(A)

struct mystruct {

};


BiTemplate(A,B)

B& operator,(mystruct With(A),B& i){return i;};


};


使用命名空间FM;


void main(void){}; //什么都不做


提前致谢,
the following compiles unless the first line is uncommented .
when I try to uncomment the first line I get:

error : ''B &FM::operator ,(FM::mystruct<A>,B &)'' : could not deduce
template argument for ''FM::mystruct<A>'' from
''std::basic_string<_Elem,_Traits,_Ax>::_Myt''
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

As long as ''mystruct'' is a normal struct no error occures,but there is
some trouble with template version.

error refers to to the ''assign'' method of
''std::basic_string<_Elem,_Traits,_Ax>'' :

_Myt& assign(const _Myt& _Right, size_type _Roff, size_type _Count)
{ // assign _Right [_Roff, _Roff + _Count)
...//some code here
if (this == &_Right)
erase((size_type)(_Roff + _Num)), erase(0, _Roff);// substring
...//some code here
}

I am confused since ''mystruct'' does not declare any ctors and normally
the compiler should not try to cast anything to it . So, after failure
to find the appropriate overload , the default version must be used .
Is there any problem with my compiler or I have to declare a default
version? I mean some thing like this:

template <typename A, typename B>
inline B& operator,(A &,B& b){return b;};//default comma in global
namespace

Is there any restriction on overloading comma?
What are the semantics for overloading comma?

//#define uncomment
#include <iostream>

#ifdef uncomment/*template version of mystruct*/
# define BiTemplate(A,B) template < typename A ,typename B >
# define UnoTemplate(A) template < typename A >
# define With(A) < A >
#else/*none template version of mystruct: ignore A .*/
# define BiTemplate(A,B) template < typename B >
# define UnoTemplate(A)
# define With(A)
#endif

namespace FM{

UnoTemplate(A)
struct mystruct{
};

BiTemplate(A,B)
B& operator,(mystruct With(A) ,B& i){return i;};

};

using namespace FM;

void main(void){};//just do nothing

Thanks in advance,



您使用的是Microsoft Visual Studio Express 2005吗?我在这里看到了相同的

错误

(不过你的代码)。由于某些原因,似乎VS没有正确应用SFINAE

(我猜)。或者它以某种方式完全相关

不相关的代码(你的逗号与普通的系统逗号,应该是

使用

in assign)。


我认为这是编译器中的一个错误。


问候

Jiri Palecek


8月4日晚上7:06,jpale ... @ web.de写道:
On Aug 4, 7:06 pm, jpale...@web.de wrote:

On 4 Srp ,13:54,终结者< farid.mehr ... @ gmail.comwrote:


On 4 Srp, 13:54, terminator <farid.mehr...@gmail.comwrote:



以下编译,除非第一行取消注释。

当我尝试取消注释第一行时:
the following compiles unless the first line is uncommented .
when I try to uncomment the first line I get:


错误:''B& FM :: operator,(FM :: mystruct< A>,B&)'':无法从
中推断''FM :: mystruct< A>''的
模板参数
''std :: basic_string< _Elem,_Traits,_Ax> :: _ Myt''

with

[

_Elem = char,

_Traits = std :: char_traits< char>,

_Ax = std :: allocator< char>

]
error : ''B &FM::operator ,(FM::mystruct<A>,B &)'' : could not deduce
template argument for ''FM::mystruct<A>'' from
''std::basic_string<_Elem,_Traits,_Ax>::_Myt''
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]


只要''mystruct''是一个普通的struct就没有错误发生,但是

模板版有些麻烦。
As long as ''mystruct'' is a normal struct no error occures,but there is
some trouble with template version.


错误是指

''std :: basic_string< _Elem,_Traits的''assign''方法,_Ax>'':
error refers to to the ''assign'' method of
''std::basic_string<_Elem,_Traits,_Ax>'' :


_Myt& assign(const _Myt& _Right,size_type _Roff,size_type _Count)

{// assign _Right [_Roff,_Roff + _Count)

... //这里的一些代码

if(this ==& _Right)

erase((size_type)(_ Roff + _Num)),erase(0,_ Roff); // substring

... //这里的一些代码

}
_Myt& assign(const _Myt& _Right, size_type _Roff, size_type _Count)
{ // assign _Right [_Roff, _Roff + _Count)
...//some code here
if (this == &_Right)
erase((size_type)(_Roff + _Num)), erase(0, _Roff);// substring
...//some code here
}


我很困惑,因为''mystruct''没有声明任何ctors,通常

编译器不应该尝试向它投射任何东西。因此,在失败后找到适当的重载,必须使用默认版本。

我的编译器是否有任何问题,或者我必须声明默认值

版本?我的意思是这样的事情:
I am confused since ''mystruct'' does not declare any ctors and normally
the compiler should not try to cast anything to it . So, after failure
to find the appropriate overload , the default version must be used .
Is there any problem with my compiler or I have to declare a default
version? I mean some thing like this:


template< typename A,typename B>

inline B& operator,(A&,B& b){return b;}; //全局默认逗号

命名空间
template <typename A, typename B>
inline B& operator,(A &,B& b){return b;};//default comma in global
namespace


重载逗号是否有任何限制?

重载逗号的语义是什么?
Is there any restriction on overloading comma?
What are the semantics for overloading comma?


// #define uncomment

#include< iostream>
//#define uncomment
#include <iostream>


#ifdef取消注释/ * mystruct的模板版本* /

#define BiTemplate(A,B)模板< typename A,typename B>

#define UnoTemplate(A)template< typename A>

#define with(A)< A>

#else / *无模板版本的mystruct:忽略A。* /

#define BiTemplate(A,B)模板< typename B>

#define UnoTemplate(A)

#define with(A)

#endif
#ifdef uncomment/*template version of mystruct*/
# define BiTemplate(A,B) template < typename A ,typename B >
# define UnoTemplate(A) template < typename A >
# define With(A) < A >
#else/*none template version of mystruct: ignore A .*/
# define BiTemplate(A,B) template < typename B >
# define UnoTemplate(A)
# define With(A)
#endif


namespace FM {
namespace FM{


UnoTemplate(A)

struct mystruct {

};
UnoTemplate(A)
struct mystruct{
};


BiTemplate(A,B)

B& operator,(mystruct With(A),B& i){return i;};
BiTemplate(A,B)
B& operator,(mystruct With(A) ,B& i){return i;};


};
};


使用名称空间FM;
using namespace FM;


void main(void){}; //什么都不做
void main(void){};//just do nothing


提前致谢,
Thanks in advance,



您使用的是Microsoft Visual Studio Express 2005吗?我在这里看到了相同的

错误

(不过你的代码)。由于某些原因,似乎VS没有正确应用SFINAE

(我猜)。或者它以某种方式完全相关

不相关的代码(你的逗号与普通的系统逗号,应该是

使用

in assign)。


我认为这是编译器中的一个错误。


Are you using Microsoft Visual Studio Express 2005? I see the same
error
here (not with your code, though). It seems VS is not applying SFINAE
correctly for some reason (I guess). Or it is somehow relating totally
unrelated code (your comma vs. normal ystem comma, which should be
used
in assign).

I think it is a bug in compiler.



我想是的。


非常感谢,

FM。

I guess so.

thanks a lot,
FM.


这篇关于逗号超载语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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