是否可以让编译器自动将我的Enum值转换为字符串,这样我就可以避免每次都显式调用ToString方法.这是我想做的一个例子:
enum Rank { A, B, C }
Rank myRank = Rank.A;
string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string'
string myString2 = Rank.A.ToString(); // OK: but is extra work
Run Code Online (Sandbox Code Playgroud) 在广泛阅读ISO/IEC 14882,编程语言 - C++后,我仍然不确定为什么const需要使用单个参数构造函数隐式转换为用户定义的类型,如下所示
#include <iostream>
class X {
public:
X( int value ) {
printf("constructor initialized with %i",value);
}
}
void implicit_conversion_func( const X& value ) {
//produces "constructor initialized with 99"
}
int main (int argc, char * const argv[]) {
implicit_conversion_func(99);
}
Run Code Online (Sandbox Code Playgroud)
从第4节第3行开始
当且仅当声明T t = e时,表达式e可以隐式转换为类型T. 对于一些发明的临时变量t(8.5),其形式良好.某些语言结构要求将表达式转换为布尔值.在这样的上下文中出现的表达式e被称为在上下文中转换为bool并且当且仅当声明bool t(e)时才是格式良好的; 对于一些发明的临时变量t(8.5),其形式良好.隐式转换的效果与执行声明和初始化相同,然后使用临时变量作为转换的结果.如果T是左值引用类型(8.3.2),则结果是左值,否则为右值.当且仅当初始化将其用作左值时,表达式e用作左值.
接下来,我在8.5行6中找到了与用户定义类型相关的初始化器部分
如果程序要求对const限定类型T的对象进行默认初始化,则T应为具有用户提供的默认构造函数的类类型.
最后,我以12.3第2行结束了有关用户定义的转换的信息
用户定义的转换仅在明确无误的情况下应用(10.2,12.3.2).
不用说,10.2和12.3.2没有回答我的问题.
const隐含转换的影响吗?const12.3第2行使转换"明确"?const以某种方式影响第4节中谈到的左值与右值?如果我有一个C#类隐式转换为double,如下所示:
public class Parameter
{
private double _value;
public Parameter(double value) { _value = value }
public static implicit operator double(Parameter p) { return _value; }
}
Run Code Online (Sandbox Code Playgroud)
F#不喜欢我试图使用它,就好像它是float:
let a = Parameter(4.0)
let b = Parameter(2.0)
let c = a * Math.Sin(b) <-- 'expected float, here Parameter'
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点(我猜不会,基于这个问题/答案),如果没有,什么是一个体面的解决方法?
我很难以我能够轻易理解的方式找到关于这个主题的信息,所以我要求对我发现的内容进行审查.这只是关于转换和转换的.
在示例中,我将指的是:
(signed/unsigned) int bigger;
(signed/unsigned) char smaller;
Run Code Online (Sandbox Code Playgroud)
截断整数.(超大化>小)
biggersmaller
如果较大的值太大而不适合较小的类型,则会导致未定义的行为(纠正我).但是我的规则应该是在所有机器上工作(对此也是正确的),结果应该是可预测的.
加宽整数(小 - >更大)
a)signed char- >signed int
b)signed char- >unsigned int
c)unsigned char- >signed int
d)unsigned char- >unsigned int
哪些未定义/未指定的行为,我没有提到可以弹出?
感谢Martin Odersky在Coursera上的最新产品,我已经开始玩了scala.util.Try.但是,我很惊讶地发现它并不一定能很好地与集合monad一起使用,因为它没有实现scala.collection.GetTraversableOnce.
这可能会派上用场.例如,您可以将字符串列表转换为整数,同时抛弃坏的字符串,如下所示:
def ints(strs:List[String]):List[Int] = strs.flatMap(s => Try(s.toInt))
Run Code Online (Sandbox Code Playgroud)
解决方法很简单.只需将其转换Try为an Option并让其隐式转换为我们工作:
def ints(strs:List[String]):List[Int] = strs.flatMap(s => Try(s.toInt).toOption)
Run Code Online (Sandbox Code Playgroud)
在我看来,Try它将实现GenTraversableOnce或具有自己的隐式转换.谁能解释为什么不呢?事实上,这实际上Try并不是一个单子吗?
在下面显示的代码中,没有任何内容被打印,这意味着for循环中的条件失败.可能是什么原因?
我想知道,因为当我TOTAL_ELEMENTS单独打印时,它给出了5,所以它必须是5-2=3 => -1<=3,所以它应该打印一些东西.
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = { 23, 34, 12, 17, 204, 99, 16 };
int main()
{
int d;
for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++) {
printf("%d\n", array[d + 1]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释这段代码吗?
这是C++程序:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int test_string(const string & str) {
return str.size();
}
void main() {
test_string(""); //can compile
vector<string> v;
string sum = accumulate(v.cbegin(), v.cend(), ""); //cannot compile
}
Run Code Online (Sandbox Code Playgroud)
我想使用隐式转换,从const char *以string仿制STL函数的调用accumulate.我知道转换const char *为字符串不是显式的,因此我们可以将const char *参数传递给需要string类型的调用.这可以通过上述test_string功能证明.但当我做同样的事情时accumulate,编译器抱怨:
Run Code Online (Sandbox Code Playgroud)error C2440: '=': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'const char *'
代码工作只有当我更换""使用string("").我不明白为什么隐式转换适用于我的自定义函数但不起作用accumulate.你能解释一下吗?非常感谢.
PS:我正在使用Visual Studio 2015.
在C++中,可以在类或结构中添加隐式转换运算符.例如,3D矢量类型通常包括以下内容:
struct Vector {
float x, y, z;
operator float * () { return reinterpret_cast<float *>(this); }
};
Run Code Online (Sandbox Code Playgroud)
允许使用下标访问向量的元素,传递给需要指针等的函数.我想知道:我们可以编写一个转换运算符来返回对float数组的引用,而不是指向float的指针吗?
(这纯粹是学术上的兴趣.我不知道对于一个简单的指针,引用数组会有什么好处,如果有的话.)
作为一个免费功能,我们可以这样做:
float (&convert(Vector & v))[3]
{
return reinterpret_cast<float(&)[3]>(v);
}
Vector v;
convert(v);
Run Code Online (Sandbox Code Playgroud)
但是,我无法找到正确的语法来执行此操作作为转换运算符.我尝试过这样的事情:
operator float(&)[3] ()
operator float(&())[3]
float (&operator())[3]
Run Code Online (Sandbox Code Playgroud)
和其他各种排列,但我只是得到各种语法错误(g ++ 4.8.1).
是否可以编写一个转换运算符,返回对数组的引用,如果是,那么这样做的语法是什么?
考虑以下小程序:
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {}
int f(const A& a) { return 0; }
int f(std::vector<int> a) { return 1; }
};
int g(const A& a) { return 2; }
int g(std::vector<int> a) { return 3; }
int main() {
A a(1,2);
// a.f({}); //ambiguous according to gcc
g({}); //ambiguous according to gcc
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC 10.2 拒绝编译它:它说调用g({})和a.f({})是模棱两可的。Clang …
在以下代码中,struct A有两个隐式转换运算符到char和int,并且将结构体的实例与整数常量进行比较2:
struct A {
constexpr operator char() { return 1; }
constexpr operator int() { return 2; }
};
static_assert( A{} == 2 );
Run Code Online (Sandbox Code Playgroud)
代码在 GCC 和 MSVC 中顺利通过,但 Clang 抱怨:
<source>:5:20: error: use of overloaded operator '==' is ambiguous (with operand types 'A' and 'int')
static_assert( A{} == 2 );
~~~ ^ ~
<source>:5:20: note: because of ambiguity in conversion of 'A' to 'float'
<source>:2:15: note: candidate function
constexpr operator …Run Code Online (Sandbox Code Playgroud)