简体   繁体   English

具有混合数据类型的条件运算符?

[英]Conditional operator with mixed data types?

Conditional operator cannot work with mixed data types, so: 条件运算符不能使用混合数据类型,因此:

bool cond = true;
cout << (cond?1:2) << endl;
cout << (cond?"msg1":"msg2") << endl;
cout << (cond?1:"msg") << endl;

On the last line, I get this error message: 在最后一行,我收到此错误消息:

error: incompatible operand types ('int' and 'const char *') 错误:不兼容的操作数类型('int'和'const char *')

Is there a way to mix different types in such a statement, using a single line of code? 有没有办法在这样的语句中混合使用单行代码的不同类型? I need to put it inside a preprocessor macro. 我需要将它放在预处理器宏中。

Compiler: clang 3.5 编译:clang 3.5

The problem with this is that the ? 这个问题是? : operators result in an assignment operation thus it can only assign the value to a single type and at compile time it is not possible to determine which one of the types should be used since the code branches as if an if statement is used. :运算符导致赋值操作,因此它只能将值赋值给单个类型,并且在编译时不可能确定应该使用哪种类型,因为代码分支就好像使用了if语句一样。 Imagine a method that returns a string in one case and an int in other, that's just not possible. 想象一个方法,在一个case中返回一个字符串,在另一个case中返回一个int,这是不可能的。 What you can do is you can even out the types as @Vlad from Moscow suggested. 你可以做的就是你可以在莫斯科建议的@Vlad中输出类型。

For the last statement you can determine the common type like std::string . 对于最后一个语句,您可以确定常见类型,如std::string

For example 例如

std::cout << ( cond ? std::to_string( 1 ) : "msg" ) << std::endl;

The << operator is overloaded for different data types, and when the code is compiled the compiler determines what overload function to call. 对于不同的数据类型,<<运算符被重载,并且在编译代码时,编译器确定要调用的重载函数。 This is why you can't have a function that return different possible datatypes: The compiler can't choose the right overload that way. 这就是为什么你不能有一个返回不同可能数据类型的函数的原因:编译器不能选择正确的重载方式。

So, see (cond ? result1 : result2) as a function that requires exactly one return type, like any other function. 因此,请将(cond?result1:result2)视为一个只需要一个返回类型的函数,就像任何其他函数一样。

If you give a clearer example of what exactly you're trying to do, it might be easier to help you with it! 如果您更清楚地说明您正在尝试做什么,可能更容易帮助您!

Cheers 干杯

statement (cond?1:"msg") during compilation it self will leads to failure . 声明(cond?1:"msg")在编译期间它会导致失败。

let a = (cond?1:"msg") Compiler will translate this expression similar to this a = (cond?1:"msg")编译器将这个表达式翻译成类似于此

if cond then 
    a = 1
  else
   a ="msg"

As we can see that type of value assigned to a is changed from if to else. 我们可以看到分配给a的值的类型从if更改为else。 But if you are in dynamic language that supports type changing then it will be valid. 但如果您使用支持类型更改的动态语言,那么它将是有效的。

So you should use cout<<(cond?std::to_string( 1 ):"msg"); 所以你应该使用cout<<(cond?std::to_string( 1 ):"msg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有不同类型的C ++条件运算符 - C++ Conditional operator with different types as arguments 使用三元条件运算符时,不兼容的操作数类型 - Incompatible operand types when using ternary conditional operator 模板化的get方法可从表中检索混合数据类型 - Templated get method to retrieve mixed data types from table 如何将Modulus Operator与其他数据类型一起使用? - How to use Modulus Operator with other data types? 有没有办法针对不同的数据类型优化 operator+= - Is there a way to optimize the operator+= for different data types 在 cpp 中为 double 和 string 数据类型定义运算符 + - defining operator + for double and string data types in cpp 条件运算符中的运算符优先级 - Operator Precedence in Conditional Operator 使用stringstream和流运算符(&gt;&gt;)将缓冲区数据放入特定的数据类型? - Using stringstream and stream operator (>>) to put buffer data into specific data types? 为什么条件/三元运算符的结果可能与某些 c++ 类型中的 if-else 语句不同? - Why the result of conditional / ternary operator may differ from if-else statements in some c++ types? 条件运算符:从“int”转换为“unsigned char”,可能丢失数据 - Conditional operator: conversion from 'int ' to 'unsigned char ', possible loss of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM