(类型)值和类型(value)有什么区别?

发布于 2025-02-06 14:01:49 字数 112 浏览 3 评论 0原文

什么区别

(type)value

C ++中有

type(value)

What is the difference between

(type)value

and

type(value)

in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮凉 2025-02-13 14:01:49

没有区别;根据标准(§5.2.3):

一个简单类型的分类器(7.1.5),然后是括号表达列表构造给定表达式列表的指定类型的值。如果表达式列表是单个表达式,则类型转换表达式等效(定义性,并在含义中定义)与相应的铸造表达式(5.4)。

由于该问题指定了类型(value)(type)value之间的差异,因此绝对没有区别。

仅当您要处理值的逗号分隔列表时,才有差异。在这种情况下:

如果表达式列表指定多于单个值,则该类型应为具有适当声明的构造函数(8.5,12.1)的类(x1,x2,...);对于某些发明的临时变量t,结果是t作为rvalue的值。

正如Troubadour指出的那样,type(value)版本的某些类型名称根本不会编译。例如:

char *a = (char *)string;

将编译,但是:

char *a = char *(string);

不会。具有不同名称的相同类型(例如,用typedef创建)可以工作:

typedef char *char_ptr;

char *a = char_ptr(string);

There is no difference; per the standard (§5.2.3):

A simple-type-specifier (7.1.5) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4).

Since the question specified the difference between type(value) and (type)value, there is absolutely no difference.

If and only if you're dealing with a comma-separated list of values can there be a difference. In this case:

If the expression list specifies more than a single value, the type shall be a class with a suitably declared constructor (8.5, 12.1), and the expression T(x1, x2, ...) is equivalent in effect to the declaration T t(x1, x2, ...); for some invented temporary variable t, with the result being the value of t as an rvalue.

As Troubadour pointed out, there are a certain names of types for which the type(value) version simply won't compile. For example:

char *a = (char *)string;

will compile, but:

char *a = char *(string);

will not. The same type with a different name (e.g., created with a typedef) can work though:

typedef char *char_ptr;

char *a = char_ptr(string);
最单纯的乌龟 2025-02-13 14:01:49

没有区别; C ++标准(1998年和2003年版)很清楚这一点。尝试以下程序,请确保您使用合规的编译器,例如 http://comeaucomputing.com/ tryitout/

#include <cstdlib>
#include <string>
int main() {
  int('A'); (int) 'A'; // obvious
  (std::string) "abc"; // not so obvious
  unsigned(a_var) = 3; // see note below
  (long const&) a_var; // const or refs, which T(v) can't do
  return EXIT_SUCCESS;
}

注意:unsigned(a_var)是不同的,但确实显示了这些准确的令牌可能意味着其他的方式。它声明一个名为a_var的变量, type nosigned,根本不是铸件。 (如果您熟悉功能或数组的指针,请考虑如何在p中使用void(*pf)()()()或<p代码> int(*pa)[42]。)

(发出警告,因为这些语句不使用该值,并且在实际程序中几乎可以肯定是错误的,但一切仍然有效。我只是使一切都排队后,没有心来改变它。)

There is no difference; the C++ standard (1998 and 2003 editions) is clear about this point. Try the following program, make sure you use a compiler that's compliant, such as the free preview at http://comeaucomputing.com/tryitout/.

#include <cstdlib>
#include <string>
int main() {
  int('A'); (int) 'A'; // obvious
  (std::string) "abc"; // not so obvious
  unsigned(a_var) = 3; // see note below
  (long const&) a_var; // const or refs, which T(v) can't do
  return EXIT_SUCCESS;
}

Note: unsigned(a_var) is different, but does show one way those exact tokens can mean something else. It is declaring a variable named a_var of type unsigned, and isn't a cast at all. (If you're familiar with pointers to functions or arrays, consider how you have to use a parens around p in a type like void (*pf)() or int (*pa)[42].)

(Warnings are produced since these statements don't use the value and in a real program that'd almost certainly be an error, but everything still works. I just didn't have the heart to change it after making everything line up.)

手长情犹 2025-02-13 14:01:49

两者都是演员时没有区别,但是有时“类型(值)”不是演员。

以下是标准N3242草案,第8.2.1节的示例:

struct S 
{
    S(int);
};

void foo(double a) 
{
    S w( int(a) ); // function declaration
    S y( (int)a ); // object declaration
}

在这种情况下,“ int(a)”不是铸件,因为“ a”不是一个值,它是一个被冗余括号所包围的参数名称。文件指出

由功能风格之间的相似性产生的歧义
在上下文中也可以发表6.8中提到的声明和声明
声明。在这种情况下,选择是在函数之间
围绕参数的冗余括号的声明
名称和对象声明,用功能风格的铸造为
初始化器。就像6.8中提到的歧义一样
决议是考虑任何可能是一个可能是
声明声明。

There is no difference when both are casts, but sometimes 'type(value)' is not a cast.

Here's an example from standard draft N3242, section 8.2.1:

struct S 
{
    S(int);
};

void foo(double a) 
{
    S w( int(a) ); // function declaration
    S y( (int)a ); // object declaration
}

In this case 'int(a)' is not a cast because 'a' is not a value, it is a parameter name surrounded by redundant parentheses. The document states

The ambiguity arising from the similarity between a function-style
cast and a declaration mentioned in 6.8 can also occur in the context
of a declaration. In that context, the choice is between a function
declaration with a redundant set of parentheses around a parameter
name and an object declaration with a function-style cast as the
initializer. Just as for the ambiguities mentioned in 6.8, the
resolution is to consider any construct that could possibly be a
declaration a declaration.

梦过后 2025-02-13 14:01:49

在C中,没有类型(value),而在C/C ++中,type(value) and (type)value

In c there is no type (value), while in c/c++ both type (value) and (type) value are allowed.

一抹微笑 2025-02-13 14:01:49

为了在C ++中说明您的选择(只有一个安全检查)

#include<boost/numeric/conversion/cast.hpp> 

using std::cout;
using std::endl;
int main(){

    float smallf = 100.1;

    cout << (int)smallf << endl; // outputs 100 // c cast
    cout << int(smallf) << endl; // outputs 100 // c++ constructor = c cast

    cout << static_cast<int>(smallf) << endl; // outputs 100
//  cout << static_cast<int&>(smallf) << endl; // not allowed
    cout << reinterpret_cast<int&>(smallf) << endl; // outputs 1120416563
    cout << boost::numeric_cast<int>(smallf) << endl; // outputs 100

    float bigf = 1.23e12;

    cout << (int)bigf << endl; // outputs -2147483648
    cout << int(bigf) << endl; // outputs -2147483648

    cout << static_cast<int>(bigf) << endl; // outputs -2147483648
//  cout << static_cast<int&>(bigf) << endl; // not allowed
    cout << reinterpret_cast<int&>(bigf) << endl; // outputs 1401893083
    cout << boost::numeric_cast<int>(bigf) << endl; // throws bad numeric conversion
}

To illustrate your options in C++ (only one has a safety check)

#include<boost/numeric/conversion/cast.hpp> 

using std::cout;
using std::endl;
int main(){

    float smallf = 100.1;

    cout << (int)smallf << endl; // outputs 100 // c cast
    cout << int(smallf) << endl; // outputs 100 // c++ constructor = c cast

    cout << static_cast<int>(smallf) << endl; // outputs 100
//  cout << static_cast<int&>(smallf) << endl; // not allowed
    cout << reinterpret_cast<int&>(smallf) << endl; // outputs 1120416563
    cout << boost::numeric_cast<int>(smallf) << endl; // outputs 100

    float bigf = 1.23e12;

    cout << (int)bigf << endl; // outputs -2147483648
    cout << int(bigf) << endl; // outputs -2147483648

    cout << static_cast<int>(bigf) << endl; // outputs -2147483648
//  cout << static_cast<int&>(bigf) << endl; // not allowed
    cout << reinterpret_cast<int&>(bigf) << endl; // outputs 1401893083
    cout << boost::numeric_cast<int>(bigf) << endl; // throws bad numeric conversion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文