显式typedef [英] Explicit typedef

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

问题描述

请纠正任何误解,或者对此有些担忧。

a一般的愚蠢想法。


我今天想知道:为什么不是C ++有明确的typedef?

显式typedef unsigned int ScreenXCoord;

显式typedef unsigned int ScreenYCoord;

显式typedef unsigned int WindowXCoord;

显式typedef unsigned int WindowYCoord;


ScreenXCoord sx1,sx2;

ScreenYCoord sy1,sy2;

WindowXCoord wx1,wx2;

WindowYCoord wy1,wy2;

....

sx1 = sx2; // ok

sy1 = sy2; // ok

wx1 = wx2; // ok

wy1 = wy2; // ok


sx1 = wy1; //错误,将WindowYCoord转换为ScreenXCoord


这样,ScreenXCoord!= ScreenYCoord!= WindowXCoord!=

WindowYCoord。


很多时候,可能不清楚特定值

指的是什么类型的东西,即使知道它的类型,特别是整数,

可能意味着宽度,高度,字节数,英寸,工资单中的天数

期间,或其他任何东西。这就是匈牙利符号发明的原因:bbCount是字节数,wxWidth是窗口宽度

坐标等等。


显然,typedef是解决方案的一部分 - 很明显

a变量声明为ScreenXCoord。但是程序员没有停止向该变量分配一个非typedef'的unsigned int或另一个
unsde int的typedef



在我看来,解决方案是将明确的

关键字的支持添加到typedef。它可能类似于C ++中的枚举,

在某些情况下可从其底层类型转换,但不是

其他。


再一次,请发表任何批评(或称赞,如果我真的没有'b $ b忽略了什么)。

Please correct any misconceptions, or voice concerns about this being
a stupid idea in general.

I was wondering today: why doesn''t C++ have explicit typedefs?

explicit typedef unsigned int ScreenXCoord;
explicit typedef unsigned int ScreenYCoord;
explicit typedef unsigned int WindowXCoord;
explicit typedef unsigned int WindowYCoord;

ScreenXCoord sx1, sx2;
ScreenYCoord sy1, sy2;
WindowXCoord wx1, wx2;
WindowYCoord wy1, wy2;
....
sx1 = sx2; // ok
sy1 = sy2; // ok
wx1 = wx2; // ok
wy1 = wy2; // ok

sx1 = wy1; // error, converting WindowYCoord to ScreenXCoord

This way, ScreenXCoord != ScreenYCoord != WindowXCoord !=
WindowYCoord.

Many times, it can be unclear what sort of thing a particular value
refers to, even knowing its type, especially with integers, which
could mean width, height, count of bytes, inches, days in a payroll
period, or just about anything else. This is why Hungarian notation
was invented -- cbCount is count of bytes, wxWidth is width in window
coordinates, etc.

Obviously, typedefs are one part of the solution -- it''s obvious what
a variable declared as ScreenXCoord is for. But there''s no stopping
the programmer from assigning a non-typedef''d unsigned int or another
typedef of unsigned int to that variable.

It seems to me the solution would be to add support for the explicit
keyword to typedef. It would be something like enum in C++,
convertible from their underlying type in some cases, but not the
others.

Once again, please post any criticism (or praise, if I really haven''t
overlooked anything).

推荐答案

jl*********@gmail.com 写道:

请纠正任何误解,或者对此一般的声音问题

a愚蠢的想法。
Please correct any misconceptions, or voice concerns about this being
a stupid idea in general.



不是傻瓜,但它被称为班级!


您可以使用模板来做你喜欢的事情做。


有一个非常酷的单位库这种叫做泉的东西。

http://sourceforge.net/projects/quan


无需新的语言功能。

Not stupid but it''s called a class !

You can use templates to do what you''re doing.

There is a really cool units library this kind of stuff called Quan.

http://sourceforge.net/projects/quan

No new langauge features needed.


5月7日晚上8:33,jlong​​str ... @ gmail.com写道:
On May 7, 8:33 pm, jlongstr...@gmail.com wrote:

请更正任何误解,或者说是对这个问题的担忧

a一般的愚蠢想法。


我今天想知道:为什么C ++没有明确的typedef?


显式typedef unsigned int ScreenXCoord;

显式typedef unsigned int ScreenYCoord;

显式typedef unsigned int WindowXCoord;

显式typedef unsigned int WindowYCoord;


ScreenXCoord sx1,sx2 ;

ScreenYCoord sy1,sy2;

WindowXCoord wx1,wx2;

WindowYCoord wy1,wy2;

.. 。

sx1 = sx2; // ok

sy1 = sy2; // ok

wx1 = wx2; // ok

wy1 = wy2; // ok


sx1 = wy1; //错误,将WindowYCoord转换为ScreenXCoord


这样,ScreenXCoord!= ScreenYCoord!= WindowXCoord!=

WindowYCoord。


很多时候,可能不清楚特定值

指的是什么类型的东西,即使知道它的类型,特别是整数,

可能意味着宽度,高度,字节数,英寸,工资单中的天数

期间,或其他任何东西。这就是匈牙利符号发明的原因:bbCount是字节数,wxWidth是窗口宽度

坐标等等。


显然,typedef是解决方案的一部分 - 很明显

a变量声明为ScreenXCoord。但是程序员没有停止向该变量分配一个非typedef'的unsigned int或另一个
unsde int的typedef



在我看来,解决方案是将明确的

关键字的支持添加到typedef。它可能类似于C ++中的枚举,

在某些情况下可从其底层类型转换,但不是

其他。


再一次,请发表任何批评(或称赞,如果我真的没有/ b $ b忽略任何东西)。
Please correct any misconceptions, or voice concerns about this being
a stupid idea in general.

I was wondering today: why doesn''t C++ have explicit typedefs?

explicit typedef unsigned int ScreenXCoord;
explicit typedef unsigned int ScreenYCoord;
explicit typedef unsigned int WindowXCoord;
explicit typedef unsigned int WindowYCoord;

ScreenXCoord sx1, sx2;
ScreenYCoord sy1, sy2;
WindowXCoord wx1, wx2;
WindowYCoord wy1, wy2;
...
sx1 = sx2; // ok
sy1 = sy2; // ok
wx1 = wx2; // ok
wy1 = wy2; // ok

sx1 = wy1; // error, converting WindowYCoord to ScreenXCoord

This way, ScreenXCoord != ScreenYCoord != WindowXCoord !=
WindowYCoord.

Many times, it can be unclear what sort of thing a particular value
refers to, even knowing its type, especially with integers, which
could mean width, height, count of bytes, inches, days in a payroll
period, or just about anything else. This is why Hungarian notation
was invented -- cbCount is count of bytes, wxWidth is width in window
coordinates, etc.

Obviously, typedefs are one part of the solution -- it''s obvious what
a variable declared as ScreenXCoord is for. But there''s no stopping
the programmer from assigning a non-typedef''d unsigned int or another
typedef of unsigned int to that variable.

It seems to me the solution would be to add support for the explicit
keyword to typedef. It would be something like enum in C++,
convertible from their underlying type in some cases, but not the
others.

Once again, please post any criticism (or praise, if I really haven''t
overlooked anything).



C ++有类,在这种情况下,您处理的是什么?

轴值或坐标?

思考封装。


生成坐标点,将其移动到特定位置。

在那里生成一个屏幕坐标,再次移动原点,生成

a那里的窗口坐标。

窗口坐标可以从屏幕坐标创建但不是

反之亦然。


#include< iostream>


模板< typename T = unsigned>

class Coord

{

T x;

T y;

public:

// def参数化ctor

Coord(const T x_ = T(),const T y_ = T())

:x(x_),y(y_){}

//编译器定义的复制ctor将执行

//如果您打算使用虚拟dtor Coord * ptrs

T getX()const {return x; }

T getY()const {return y; }

void move(const T x_,const T y_)

{

x + = x_;

y + = y_;

}

受保护:

void set(const T x_,const T y_)

{

x = x_;

y = y_;

}

朋友std :: ostream&

运算符<<(std :: ostream& os,const Coord& r_c)

{

os<< x = << r_c.x;

os<< " \ty =" << r_c.y;

返回操作系统;

}

};


模板< typename T = unsigned>

class ScreenCoord:public Coord< T>

{

public:

显式ScreenCoord(const Coord< T>& c)

:Coord< T>(c){}

};


模板< typename T = unsigned>

class WindowCoord:public Coord< T>

{

public:

显式WindowCoord(const Coord< T>& c)

:Coord< T>(c){}

//转换op =

WindowCoord& operator =(const ScreenCoord< T rhv)

{

set(rhv.getX(),rhv.getY());

return *这个;

}

};


typedef Coord< u_coord;

typedef ScreenCoord< u_screencoord;

typedef WindowCoord< u_windowcoord;


int main()

{

u_coord point;

std :: cout << 点: <<点<< std :: endl;


std :: cout<< " point.move(+ 10,+ 20)\ n";

point.move(+ 10,+ 20);


u_screencoord sc (点);

std :: cout<< screencoord: << sc<< std :: endl;


std :: cout<< " point.move(+ 20,+ 5)\ n";

point.move(+ 20,+ 5);


u_windowcoord wc (点);

std :: cout<< windowcoord: << wc<< std :: endl;


std :: cout<< " \ nwindowcoord = screencoord \ n";

wc = sc; // ok

std :: cout<< wc<< std :: endl;


// sc = wc; //错误,没有转换=可用

}


/ *

点:x = 0 y = 0

point.move(+ 10,+ 20)

screencoord:x = 10 y = 20

point.move(+ 20,+ 5)

windowcoord:x = 30 y = 25

windowcoord = screencoord

x = 10 y = 20

* /


___

如果有一天客户代码出现以下错误:


u_coord pt ;

u_coord另一个(pt.getY(),pt.getY());


建议自然的选择 - 复制ctor:


u_coors pt;

u_coord(pt);


现在你有了一个系统,而不仅仅是一些类型的dede这意味着

anthing。

C++ has classes, and in this case, what *are) you dealing with?
axis values or coordinates?
Think encapsulation.

Generate a coordinate point, move it to a specific location.
Generate a Screen Coord there, move the original point again, generate
a Window coordinate there.
A window coordinate can be created from a screen coordinate but not
vice-versa.

#include <iostream>

template< typename T = unsigned >
class Coord
{
T x;
T y;
public:
// def parametized ctor
Coord(const T x_ = T(), const T y_ = T())
: x(x_), y(y_) { }
// compiler defined copy ctor will do
// virtual dtor required if you plan to use Coord* ptrs
T getX() const { return x; }
T getY() const { return y; }
void move(const T x_, const T y_)
{
x += x_;
y += y_;
}
protected:
void set(const T x_, const T y_)
{
x = x_;
y = y_;
}
friend std::ostream&
operator<<(std::ostream& os, const Coord& r_c)
{
os << "x = " << r_c.x;
os << "\ty = " << r_c.y;
return os;
}
};

template< typename T = unsigned >
class ScreenCoord : public Coord< T >
{
public:
explicit ScreenCoord(const Coord< T >& c)
: Coord< T >(c) { }
};

template< typename T = unsigned >
class WindowCoord : public Coord< T >
{
public:
explicit WindowCoord(const Coord< T >& c)
: Coord< T >(c) { }
// conversion op=
WindowCoord& operator=(const ScreenCoord< T rhv)
{
set(rhv.getX(), rhv.getY());
return *this;
}
};

typedef Coord< u_coord;
typedef ScreenCoord< u_screencoord;
typedef WindowCoord< u_windowcoord;

int main()
{
u_coord point;
std::cout << "point: " << point << std::endl;

std::cout << "point.move(+10, +20)\n";
point.move(+10, +20);

u_screencoord sc( point );
std::cout << "screencoord: " << sc << std::endl;

std::cout << "point.move(+20, +5)\n";
point.move(+20, +5);

u_windowcoord wc( point );
std::cout << "windowcoord: " << wc << std::endl;

std::cout << "\nwindowcoord = screencoord\n";
wc = sc; // ok
std::cout << wc << std::endl;

// sc = wc; // error, no conv op= available
}

/*
point: x = 0 y = 0
point.move(+10, +20)
screencoord: x = 10 y = 20
point.move(+20, +5)
windowcoord: x = 30 y = 25

windowcoord = screencoord
x = 10 y = 20
*/

___
And if one day a client codes the following mistake:

u_coord pt;
u_coord another(pt.getY(), pt.getY());

suggest the natural alternative - copy ctor:

u_coors pt;
u_coord( pt );

And now you have a system, not just a few typedefs that don''t mean
anthing.


On 8 Maj,03:51,Gianni Mariani< gi3nos ... @ mariani.wswrote:
On 8 Maj, 03:51, Gianni Mariani <gi3nos...@mariani.wswrote:

jlong​​str ... @ gmail.com写道:
jlongstr...@gmail.com wrote:

请更正任何误解或语音概念关于这个问题,总的来说,这是一个愚蠢的想法。
Please correct any misconceptions, or voice concerns about this being
a stupid idea in general.



不是傻瓜,但它被称为班级!


您可以使用模板来做你喜欢的事情这样做。


Not stupid but it''s called a class !

You can use templates to do what you''re doing.



我不确定我完全同意,

最常见的用法之一这样的typedef可能会创建新的基于

内置类型的类型,虽然可以创建一个完全模仿int的类,但它会(相对)非常多工作在

比例的福利。一旦我想要第二种类型的
类型,也表现得像一个int我必须复制整个类。


使用模板你可以使用一个类定义,

,但你仍然需要一个定义。并且始终存在风险

,编译器无法优化包装(也许

不是很大的风险,但无论如何)。我想出了以下内容,也许

有人有更好的解决方案吗?


模板< int N>

class MyInt {

int data;

public:

MyInt(int i):data(i){}

MyInt(const MyInt< N>& i):data(i.data){}

MyInt< N>& operator =(const MyInt< N>& i){data = i.data;返回

* this; }

/ *操作等* /

};


int main()

{

typedef MyInt< 1length;

typedef MyInt< 2volume;


长度x = 5;

体积y = 1;

y = x; //失败

}


我不是说它无法完成,但它会更容易

让编译器理解typedef定义的两种类型不兼容




-

Erik Wikstr?m

I''m not sure I completely agree, one of the most common usages for
such a typedef would probably be to create new types based on the
built-in types, and while it''s possible to create a class that
perfectly mimics an int, it would be (relatively) very much work in
proportion to the benefits. And as soon as I wanted a second kind of
type, also behaving like an int I''d have to copy the whole class.

Using templates you can get away with using just one class definition,
but you still need that one definition. And there is always the risk
that the compiler won''t be able to optimize away the wrapping (perhaps
not a big risk but anyway). I''ve come up with the following, perhaps
someone have a better solution?

template<int N>
class MyInt{
int data;
public:
MyInt(int i) : data(i) { }
MyInt(const MyInt<N>& i) : data(i.data) {}
MyInt<N>& operator=(const MyInt<N>& i) { data = i.data; return
*this; }
/*operations etc*/
};

int main()
{
typedef MyInt<1length;
typedef MyInt<2volume;

length x = 5;
volume y = 1;
y = x; // Fail
}

I''m not saying it could not be done, but it would be much easier to
let the compiler understand that two types defined by typedefs are not
compatible.

--
Erik Wikstr?m


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

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