试图申请SFINAE [英] Trying to apply SFINAE

查看:48
本文介绍了试图申请SFINAE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有两个重载的功能模板,


#include< iterator>


模板< typename T>

void test(T / * a1 * /,T / * a2 * /){}


template< typename Iter>

无效测试(Iter / * b * /,Iter / * e * /){}


我需要打电话。 (实际上,这些都是构造函数,

,如果重要的话。)不幸的是,编译器并不是很聪明而且不能告诉''T' 'from''Iter'',所以我必须

给它一个暗示是否通过的是迭代器。

但是我尝试使用SFINAE来实现这个目的/>

模板< typename Iter,class ItTr>

void test(Iter / * b * /,Iter / * e * /

,ItTr = std :: iterator_traits< Iter>( )}}}}
不起作用,因为这完全从编译器考虑的集合中删除了迭代器

重载。或者那是'

我认为是因为这个


#include< iterator>


//模板< typename T>

// void test(T / * a1 * /,T / * a2 * /){}


template< typename Iter,class ItTr>

void test(Iter / * b * /,Iter / * e * /

,ItTr = std :: iterator_traits< Iter>( ))}} {}

int main()

{

const int fa [] = {255,255,255, 255};


// test(0,1);

test(fa,fa + 4);


返回0;

}


无法编译。

现在我很难过。我以为我之前已经这样做了,所以我很可能在这里做了一些愚蠢的事情。 (已经通过了

这里下午6点...)


任何人在那里?


TIA ,


Schobi

Hi,

I''m having two overloaded function templates,

#include <iterator>

template< typename T >
void test( T /*a1*/, T /*a2*/ ) {}

template< typename Iter >
void test( Iter /*b*/, Iter /*e*/ ) {}

which I need to call. (In reality, these are constructors,
in case that matters.) Unfortunately, the compiler isn''t
as clever and can''t tell ''T'' from ''Iter'', so I have to
give it a hint whether what gets passed is an iterator.
But my attempt to use SFINAE for this

template< typename Iter, class ItTr >
void test( Iter /*b*/, Iter /*e*/
, ItTr = std::iterator_traits<Iter>() ) {}

doesn''t work, since this completely removed the iterator
overload from the set the compiler considered. Or that''s
what I figured because this

#include <iterator>

//template< typename T >
//void test( T /*a1*/, T /*a2*/ ) {}

template< typename Iter, class ItTr >
void test( Iter /*b*/, Iter /*e*/
, ItTr = std::iterator_traits<Iter>() ) {}

int main()
{
const int fa[] = { 255, 255, 255, 255 };

//test(0,1);
test(fa, fa+4);

return 0;
}

doesn''t compile.
Now I''m stumped. I thought I had done this before, so I am
probably making something stupid here. (It''s already passed
6pm here...)

Anyone out there?

TIA,

Schobi

推荐答案

Hendrik Schober写道:
Hendrik Schober wrote:

[..]

#include< iterator>


//模板< typename T>

// void test(T / * a1 * /,T / * a2 * /){}


template< typename Iter,class ItTr>

void test(Iter / * b * /,Iter / * e * /

,ItTr = std :: iterator_traits< Iter>( ))}} {}

int main()

{

const int fa [] = {255,255,255, 255};


// test(0,1);

test(fa,fa + 4);


返回0;

}


无法编译。

[..]
[..]
#include <iterator>

//template< typename T >
//void test( T /*a1*/, T /*a2*/ ) {}

template< typename Iter, class ItTr >
void test( Iter /*b*/, Iter /*e*/
, ItTr = std::iterator_traits<Iter>() ) {}

int main()
{
const int fa[] = { 255, 255, 255, 255 };

//test(0,1);
test(fa, fa+4);

return 0;
}

doesn''t compile.
[..]



好​​吧,编译器不能从默认参数中推断出类型,我很隐约地回忆起它是一个不可推导的上下文。这就是为什么把那些特质放在那里不会削减它的原因。考虑


#include< iterator>


模板< typename Iter>

void test(Iter,Iter) ,std :: iterator_traits< Iterconst * = 0);


int main()

{

const int fa [] = {1,2,3,4};

test(fa,fa + 4);

}


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

Well, the compiler cannot deduce the type from a default argument, I
vaguely recall that it''s a non-deducible context. That''s why putting
the traits there won''t cut it. Consider

#include <iterator>

template<typename Iter>
void test(Iter, Iter, std::iterator_traits<Iterconst* = 0);

int main ()
{
const int fa[] = { 1,2,3,4 };
test(fa, fa+4);
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

Hendrik Schober写道:

[...]


好​​吧,编译器无法从默认参数中推断出类型,我

依稀回忆它是's不可扣除的背景。这就是为什么把那些特质放在那里不会削减它的原因。考虑


#include< iterator>


模板< typename Iter>

void test(Iter,Iter) ,std :: iterator_traits< Iterconst * = 0);


int main()

{

const int fa [] = {1,2,3,4};

test(fa,fa + 4);

}
Hendrik Schober wrote:
[...]

Well, the compiler cannot deduce the type from a default argument, I
vaguely recall that it''s a non-deducible context. That''s why putting
the traits there won''t cut it. Consider

#include <iterator>

template<typename Iter>
void test(Iter, Iter, std::iterator_traits<Iterconst* = 0);

int main ()
{
const int fa[] = { 1,2,3,4 };
test(fa, fa+4);
}



谢谢!然而,虽然上述工作,但这不是:


#include< iterator>


模板< typename T>

void test(T / * a1 * /,T / * a2 * /){}


template< typename Iter>

void test(Iter / * b * /,Iter / * e * /

,std :: iterator_traits< Iter = std :: iterator_traits< Iter> ;()){}


int main()

{

const int fa [] = {255,255, 255,255};


test(0,1);

test(fa,fa + 4);


返回0;

}


(VC和Comeau都抱怨它不明确。)但那是'

就是我需要的。

Thanks! However, while the above works, this doesn''t:

#include <iterator>

template< typename T >
void test( T /*a1*/, T /*a2*/ ) {}

template< typename Iter >
void test( Iter /*b*/, Iter /*e*/
, std::iterator_traits<Iter= std::iterator_traits<Iter>() ) {}

int main()
{
const int fa[] = { 255, 255, 255, 255 };

test(0,1);
test(fa, fa+4);

return 0;
}

(Both VC and Comeau complain it''s ambiguous.) But that''s
just what I need.


V
V



Schobi

Schobi


James Kanze写道:
James Kanze wrote:

9月29日下午6:20,Hendrik Schober< spamt ... @ gmx.dewrote:
On Sep 29, 6:20 pm, Hendrik Schober <spamt...@gmx.dewrote:

>我有两个重载的函数模板,
>I''m having two overloaded function templates,


> #include< iterator>
> #include <iterator>


>模板< typename T>
void test(T / * a1 * /,T / * a2 * /){}
> template< typename T >
void test( T /*a1*/, T /*a2*/ ) {}


>模板< typename Iter>
void test(Iter / * b * /,Iter / * e * /){}
> template< typename Iter >
void test( Iter /*b*/, Iter /*e*/ ) {}


>其中我需要打电话。 (实际上,这些都是构造函数,
以防万一。)
>which I need to call. (In reality, these are constructors,
in case that matters.)



如果你需要初始化列表,这可能很关键。


It could be critical, if you need the initialization list.



问题更多来自你的想法:我不能

(轻松)转发电话。

The problem is more with your below idea: I cannot
(easily) forward calls.


[...]

但是当然,因为它是未定义的行为来实例化

iterator_trais所有既不是迭代器也不是一个

指针,即使类型扣除

技巧起作用,你也不能指望它。
[...]
But of course, since it''s undefined behavior to instantiate
iterator_trais with anything that is neither an iterator nor a
pointer, you couldn''t count on this even if the type deduction
trick worked.



是吗?

我认为std lib实现者必须面对这个,

但现在我看到了这个


#include< vector>

int main()

{

const unsigned int ua [] = {255,255,255,255};


std :: vector< intv1(0u,1u);

std :: vector< intv2(ua,ua + 4);


返回0;

}


与Dinkumware失败(两者都叫ctor

带迭代器),所以我一定是错的。


好​​吧,好像我回到了那么。

有什么想法吗?


Schobi

Is it?
I thought std lib implementors must have faced this,
but now I see that this

#include <vector>

int main()
{
const unsigned int ua[] = { 255, 255, 255, 255 };

std::vector<intv1(0u,1u);
std::vector<intv2(ua, ua+4);

return 0;
}

fails spectacularly with Dinkumware (both call the ctor
taking iterators), so I must be wrong.

Well, it seems I''m back to square one, then.
Any ideas out there?

Schobi


这篇关于试图申请SFINAE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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