我如何停止隐式类型转换 [英] How would I stop implicit type conversion

查看:85
本文介绍了我如何停止隐式类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板< class TClass ABC

{

public:

void SetValue(T k)

{ val = k; }


私人:

T val;

};


int main ()

{

ABC< inttmp;

tmp.SetValue(''A'');

返回0;

}


在上面的程序中,编译器创建ABC< intclass因为

变量声明tmp。


我将字符类型传递给SetValue函数,这样就可以将

从char隐式转换为int,程序运行正常。


我的疑问是,是否有一种方法可以捕获编译器对

整数类型的隐式转换,而不是平滑地从

char转换为int。我的目标是,SetValue函数不应该接受除了T之外的类型



template<class TClass ABC
{
public :
void SetValue(T k)
{ val = k; }

private:
T val;
};

int main()
{
ABC<inttmp;
tmp.SetValue(''A'');
return 0;
}

In the above program compiler creates ABC<intclass because of
variable declaration tmp.

I passed character type to SetValue function so that there will be
implicit conversion from char to int and program works fine.

My doubt is, is there a way to capture this implicit conversion of
integral types by the compiler, instead of, smoothly converting from
char to int. My aim is, SetValue function should not accept the type
other Than T.

推荐答案


sr **************** *@yahoo.com 写道:

sr*****************@yahoo.com wrote:

我的目标是,SetValue函数不应该接受除了T之外的类型


My aim is, SetValue function should not accept the type
other Than T.



#include< boost / utility / enable_if.hpp>

#include< boost / type_traits / is_same.hpp>


模板< class Tclass ABC

{

public:

template< typename T1>

typename boost :: enable_if<

boost :: is_same< T1,T>,

void

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>

template<class Tclass ABC
{
public :
template <typename T1>
typename boost::enable_if<
boost::is_same<T1, T>,
void


> :: type
>::type



SetValue(T1 k)

{val = k; }


私人:

T val;

};


问候

Andy Little

SetValue(T1 k)
{ val = k; }

private:
T val;
};

regards
Andy Little



模板< class TClass ABC
template<class TClass ABC



C ++区分大小写。你寻求的词是class,而不是Class。


C++ is case-sensitive. The word you seek is "class", rather than "Class".


{

public:

void SetValue(T k)

{val = k; }


私人:

T val;

};


int main ()

{

ABC< inttmp;

tmp.SetValue(''A'');

返回0;

}


在上面的程序中,编译器创建ABC< intclass因为

变量声明tmp。


我将字符类型传递给SetValue函数,这样就可以将

从char隐式转换为int,程序运行正常。


我怀疑是,
{
public :
void SetValue(T k)
{ val = k; }

private:
T val;
};

int main()
{
ABC<inttmp;
tmp.SetValue(''A'');
return 0;
}

In the above program compiler creates ABC<intclass because of
variable declaration tmp.

I passed character type to SetValue function so that there will be
implicit conversion from char to int and program works fine.

My doubt is,



一个更合适的词是问题,而不是怀疑。


A more suitable word would be "question", rather than "doubt".


是否有一种方法可以捕获编译器对

整数类型的隐式转换,而不是平滑地从

char转换为INT。我的目标是,SetValue函数不应该接受除了T以外的类型


is there a way to capture this implicit conversion of
integral types by the compiler, instead of, smoothly converting from
char to int. My aim is, SetValue function should not accept the type
other Than T.



我不知道任何方法 - 手。一个可能的解决方案是传递一个

指针。

-


Frederick Gotham


I don''t know of any method off-hand. A possible solution would be to pass a
pointer.
--

Frederick Gotham




kwikius写道:

kwikius wrote:
sr ***************** @ yahoo.com 写道:
sr*****************@yahoo.com wrote:

我的目标是,SetValue函数不应该接受除了T之外的类型


My aim is, SetValue function should not accept the type
other Than T.



#include< boost / utility / enable_if.hpp>

#include< boost / type_traits / is_same.hpp>


模板< class Tclass ABC

{

public:

template< typename T1>

typename boost :: enable_if<

boost: :is_same< T1,T>,

void


#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>

template<class Tclass ABC
{
public :
template <typename T1>
typename boost::enable_if<
boost::is_same<T1, T>,
void


> :: type
>::type



SetValue(T1 k)

{ val = k; }


私人:

T val;

};


问候

Andy Little

SetValue(T1 k)
{ val = k; }

private:
T val;
};

regards
Andy Little



我希望海报能给出怎么样在C ++中没有简单引用

提升。这意味着必须去查看增益是如何做到的,如果你能够理解他们的代码,那就是。


无论如何,它看起来像你从模板专业化开始。我的

猜测你可以从这样的东西开始:


template< typename T1,typename T2>

struct is_same

{

enum value = 0;

};


// T1 = T2时的模板专业化


template< typename T>

struct is_same< T,T>

{

enum value = 1;

};


和然后enable_if使用值enum进行不同的专业化。


(是谁说增强是模板实验?)

I wish posters would give "how tos" in C++ without simply quoting
boost. That means one has to go and look up how boost did it, if you
can comprehend their code, that is.

Anyway, it looks like you start off with a template specialisation. My
guess is that you can start with something like this:

template< typename T1, typename T2 >
struct is_same
{
enum value = 0;
};

// template specialisation when T1 = T2

template < typename T >
struct is_same< T, T >
{
enum value = 1;
};

and then enable_if uses the value enum for different specialisations.

(Who was it that said that boost is template experimentation?)

这篇关于我如何停止隐式类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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