在Youtube上的GoogleTechTalks视频中,Bjarne Stroustrup谈到即将推出的C++ 0x标准.在视频中,他提到了以下示例:
#include <iostream>
struct Sick
{
Sick(double d) { std::cout << d << "\n"; }
explicit Sick(int i) { std::cout << i << "\n"; }
};
int main()
{
Sick s1 = 2.1;
Sick s2(2.1);
}
Run Code Online (Sandbox Code Playgroud)
他是否意味着将explicit关键字放在之前Sick(double)而不是Sick(int),以突出显示与某些上下文中的隐式转换相关的问题?
是否可以使用带有varargs 的磁铁模式:
object Values {
implicit def fromInt (x : Int ) = Values()
implicit def fromInts(xs: Int*) = Values()
}
case class Values()
object Foo {
def bar(values: Values) {}
}
Foo.bar(0)
Foo.bar(1,2,3) // ! "error: too many arguments for method bar: (values: Values)Unit"
Run Code Online (Sandbox Code Playgroud)
?
我有隐式转换和高阶函数的问题.似乎只有转换函数至少有两个参数,函数到二阶函数的隐式转换才有效.
作品:
implicit def conv(foo: Integer => String): String => String = null
Run Code Online (Sandbox Code Playgroud)
不起作用:
implicit def conv(foo: Integer => String): String => String => String = null
Run Code Online (Sandbox Code Playgroud)
作品:
implicit def conv(foo: (Integer, Integer) => String): String => String => String = null
Run Code Online (Sandbox Code Playgroud)
完整的失败点示例:
{
implicit def conv(foo: Integer => String): String => String = null
def baadf00d(foo: Integer): String = null
def deadbeef(foo: String => String) = null
deadbeef(conv(baadf00d))
deadbeef(baadf00d)
}
{
implicit def conv(foo: Integer => String): String => …Run Code Online (Sandbox Code Playgroud) 一般问题:如果不考虑它是否是一个好主意,我如何将一个隐式转换运算符添加到已经定义的类中?例如,假设我想要unique_ptr <T>隐式转换为T*,但我不能只添加成员转换运算符,因为我无法更改unique_ptr类的定义.
选项:
是否有一些c ++巫毒可以用来实现这一点而无需创建成员函数?
答案 - 远:没有.
无法从无法在代码中修改的类型添加隐式转换.
只是......悲伤.
我可以从std :: unique_ptr派生并添加我自己的成员转换函数吗?这有什么严重的缺点吗?
Answer-So-Far:是的(来自vsoftco)
下行尚未确定.到目前为止继承自std :: unique_ptr,继承其构造函数,并声明一个隐式转换运算符,几乎没有任何需要编写的代码.
我将不得不在余生中生活吗?
答案 - 迄今为止:我们会看到......
如果我可以选择2运行而没有任何严重的副作用或负担,我会测试一段时间并报告我是否认为值得.走着瞧!
示例代码:
#include <algorithm>
#include <memory>
#include <vector>
struct MyClass
{
MyClass(int v) : value(v) {}
int value;
};
int main()
{
auto vec = std::vector<std::unique_ptr<MyClass>>();
vec.push_back(std::make_unique<MyClass>(1));
vec.push_back(std::make_unique<MyClass>(2));
// error C2664: 'void (__vectorcall *)(MyClass *)' : cannot convert argument 1 from 'std::unique_ptr<MyClass,std::default_delete<_Ty>>' to 'MyClass *'
std::for_each(std::begin(vec), std::end(vec), [](MyClass* myClass)
{
myClass->value += 3;
});
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我需要处理来自外部源的很多数字(整数或长整数).
数字可以为空.如果它们为null,我总是需要将它们转换为0.
问题似乎微不足道,但我不想写数百次:
if (someNumber == null) {
someNumber = 0;
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢它有两个原因:
我尝试了其他一些方法,可以在这里看到:
public static void main(String[] args) {
Integer zeroOrNull = new Random().nextBoolean() ? 0 : null;
// version1: this is nasty (I already mentioned why)
if (zeroOrNull == null) {
zeroOrNull = 0;
}
// version2: this seems to much for so simple task...
zeroOrNull = Optional.ofNullable(zeroOrNull).orElseGet(() -> 0);
// version3: creating an util might be considerable. Is there already such predefined util …Run Code Online (Sandbox Code Playgroud) 对于Scala中的隐式转换,我可以使用隐式转换函数
implicit def intToX(i:Int):X = new X(i)
1.myMethod // -1
Run Code Online (Sandbox Code Playgroud)
或隐含的类
implicit class X(i: Int) {
def myMethod = - i
}
1.myMethod // -1
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别吗?我什么时候应该更喜欢一个?
关于隐式转换与类型类有一个相关的问题,但它只比较隐式函数和类型类.我感兴趣的是与隐式类的区别.
我正在尝试向现有类型添加新功能(因此我可以让IDE自动为我无法控制的类型建议相关功能,例如Future[Option[A]]).我已经探索了隐式类和隐式转换来实现这一点,它们似乎都提供了相同的行为.
使用隐式类之间是否有任何有效的区别:
case class Foo(a: Int)
implicit class EnrichedFoo(foo: Foo) {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud)
并使用隐式转换:
case class Foo(a: Int)
trait Enriched {
def beep: String
}
implicit def fooToEnriched(foo: Foo) = new Enriched {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud)
我想这里的一个区别可能是第一个例子创建了一个一个类而不是一个特征,但是我可以很容易地调整隐式类来扩展一个抽象特征,例如:
case class Foo(a: Int)
trait Enriched {
def beep: String
}
implicit class EnrichedFoo(foo: Foo) extends Enriched {
def beep = "boop"
}
Foo(1).beep // "boop"
Run Code Online (Sandbox Code Playgroud) 以下代码在C++ 11中成功编译:
#include "json.hpp"
using json = nlohmann::json ;
using namespace std ;
int main(){
json js = "asd" ;
string s1 = js ; // <---- compiles fine
//string s2 = (string)js ; // <---- does not compile
}
Run Code Online (Sandbox Code Playgroud)
它包括JSON for Modern C++.一个工作示例就在这个wandbox中.
JSON变量js隐式转换为字符串.但是,如果我取消注释最后一行,这是一个显式转换,它就无法编译.编译结果在这里.
除了这个json库的特殊细微差别,你如何编写一个类,以便隐式转换工作,但一个明确的转换不起作用?
是否有某种构造函数限定符允许此行为?
在这个问题1的启发下,我创建了以下代码:
struct X {
X(int) {}
};
struct Y {
operator X() { return X{1}; }
operator int() { return 1; }
};
int main() {
X x(Y{});
}
Run Code Online (Sandbox Code Playgroud)
这段代码:
如果启用C++ 11/14,所有GCC和Clang版本(我已尝试过)会引发歧义错误,
如果启用了C++ 17,则会在某些GCC和Clang版本中引发歧义错误(例如,GCC 8.2,GCC 6.3,Clang 5),
如果启用C++ 17,则编译一些GCC和Clang版本(例如,GCC 7.3,Clang 6).
歧义源于可能的转换:
Y→ X,Y→ int和int→ X,因为编译器不知道X它应该使用哪个构造函数(复制/移动或转换int).
我想知道为什么编译器行为存在这种差异,以及标准是否说该代码应该触发错误.
1请注意,这不是重复的.在相关问题中,OP询问了如何消除歧义.我问为什么不同编译器的行为不同.
从我的包装器类开始,Pointer<Base>我只想返回指向const的指针:Base const *.
当转换Pointer<Base>为Derived const *我得到编译错误:
错误C2440:'static_cast':'指针'无法转换为'const Derived*'
(翻译自德语VS2012)
struct Base { };
struct Derived : public Base { };
template <typename T>
class Pointer {
public:
Pointer(T *t = nullptr) : p(t) { }
//operator T*() { return p; }
operator T const *() const { return p; }
template <typename U>
inline U staticCast() const { return static_cast<U>(d); }
private:
T *p;
};
int main(int argc, char *argv[]) {
Derived …Run Code Online (Sandbox Code Playgroud)