我必须实现基本相同的功能,但不同的大小.具体来说就是......
type& operator=(unsigned int);
type& operator=(unsigned long int);
type& operator=(unsigned long long int);
type& operator=(int);
type& operator=(long int);
type& operator=(long long int);
type& operator=(short int);
//so on and so forth...
Run Code Online (Sandbox Code Playgroud)
他们必须做同样的事情...(除了我应该考虑到不同的大小),主要的想法是"如果类型是最广泛使用任务的代码...否则执行转换和执行代码".是否可以通过仅使用一种方法来避免所有这样的重复代码?(我只是不希望编译器在编译时给我一些警告......).
谢谢
我知道使用auto关键字可以自动从 Rvalue 推导出变量的类型。那为什么我的代码中下面的函数片段会出现编译错误呢?
auto getName(auto str = "John Doe") {
return str;
}
Run Code Online (Sandbox Code Playgroud)
编译错误是函数原型中不允许的“自动”。我用谷歌搜索了一下,我认为auto不能在函数原型中使用。为什么这样?
请注意,这不是C++中的含义 - >意味着什么?
这个问题特定于C++ 11; 函数可以是这样的:
struct string_accumulator {
}
inline auto collect() -> string_accumulator
{
return string_accumulator();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下, - >的含义是什么?