为什么Scala无法优化以下内容:
一个.
implicit def whatever[A](a: A) = new { ... }
Run Code Online (Sandbox Code Playgroud)
至:
湾
class some$generated$name(a: A) {
...
}
implicit def whatever[A](a: A) = new some$generated$name(a)
Run Code Online (Sandbox Code Playgroud)
?
为什么在这种情况下必须使用结构类型?我希望Scala编译器能够执行这种优化,因为写入样式b太丑了(因为,1.逻辑的局部性丢失了,2.你必须不必要地为这些额外的显式类创建名称),而且a的性能要差得多比b.
#include <iostream>
using namespace std;
struct Y;
struct X
{
X(const Y&) { cout << "converting constructor" << endl; }
};
struct Y
{
operator X() { cout << "conversion function" << endl; }
};
void f(X x) {}
int main()
{
Y y;
f(y);
}
Run Code Online (Sandbox Code Playgroud)
在上面,转换函数由我的编译器(gcc 4.6.1)优先于转换构造函数,但是在标准中它声明:
用户定义的转换仅在明确无误的情况下应用
在这种情况下似乎存在歧义.任何人都可以解释这个矛盾吗?
我原本以为不能编译.几年前我也非常肯定Scott Meyers写了这个具体的例子,并说它不会编译.我错过了什么?
c++ copy-constructor language-lawyer implicit-conversion c++11
引自n3337 12.3.1/3
非显式复制/移动构造函数(12.8)是转换构造函数.隐式声明的复制/移动构造函数不是显式构造函数; 可以调用它来进行隐式类型转换.
引自ANSI ISO IEC 14882 2003
非显式复制构造函数(12.8)是转换构造函数.隐式声明的复制构造函数不是显式构造函数; 可以调用它来进行隐式类型转换.
我没有想法,怎么copy-constructor可以用于implicit类型conversions.如果它是标准中的错误打印/错误,为什么它自C++ 03标准以来没有得到纠正?任何链接和示例(如果我们可以使用它type conversions)都非常感谢.
对于这个问题,请考虑(创建)接口:
public interface ITestMe
{
string TakeInt64(long x);
}
Run Code Online (Sandbox Code Playgroud)
然后运行以下代码:
public void Test()
{
var mock1 = new Mock<ITestMe>(MockBehavior.Strict);
Expression<Func<ITestMe, string>> expr1 = x => x.TakeInt64(2);
Console.WriteLine(expr1);
mock1.Setup(expr1).Returns("OK");
var s1 = mock1.Object.TakeInt64(2L); // OK
var mock2 = new Mock<ITestMe>(MockBehavior.Strict);
Expression<Func<ITestMe, string>> expr2 = x => x.TakeInt64(DateTime.Today.Year / 1000);
Console.WriteLine(expr2);
mock2.Setup(expr2).Returns("OK");
var s2 = mock2.Object.TakeInt64(2L); // OK
var mock3 = new Mock<ITestMe>(MockBehavior.Strict);
Expression<Func<ITestMe, string>> expr3 = x => x.TakeInt64((int)(DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Tuesday"));
Console.WriteLine(expr3);
mock3.Setup(expr3).Returns("OK");
var s3 = mock3.Object.TakeInt64(2L); // OK
var mock4 = …Run Code Online (Sandbox Code Playgroud) 我在一个中定义了隐式转换object.让我们调用该对象Implicits,其中有一个隐式转换.
package com.gmail.naetmul.stackoverflow.app
object Implicits {
implicit def int_to_intEx(x: Int): IntEx = IntEx(x)
}
Run Code Online (Sandbox Code Playgroud)
这个对象在某个包中.我想在包中的每个代码com.gmail.naetmul.stackoverflow.app及其所有子包中使用这种隐式转换com.gmail.naetmul.stackoverflow.app.something.anything.everything.
所以我制作了包的对象com.gmail.naetmul.stackoverflow.app.
package com.gmail.naetmul.stackoverflow
package object app {
import com.gmail.naetmul.stackoverflow.app.Implicits._
}
Run Code Online (Sandbox Code Playgroud)
但它不能在确切的包对象之外工作.所以我将对象更改Implicits为trait并让包对象扩展它.
package com.gmail.naetmul.stackoverflow.app
trait Implicits {
implicit def int_to_intEx(x: Int): IntEx = IntEx(x)
}
Run Code Online (Sandbox Code Playgroud)
package com.gmail.naetmul.stackoverflow
import com.gmail.naetmul.stackoverflow.app.Implicits
package object app extends Implicits {
// some code
}
Run Code Online (Sandbox Code Playgroud)
隐式转换在包中有效com.gmail.naetmul.stackoverflow.app.但是,它在子包中工作或不工作.
例如)
文件 A.scala
package com.gmail.naetmul.stackoverflow.app.database
class A {
// Here, …Run Code Online (Sandbox Code Playgroud) 我需要在源代码中找到所有这些地方,其中隐式转换任何类型的指针void *或停止这些隐式转换的方法.
例如:
int * 至 void *char * 至 void *Base * 至 void *是否有任何gcc警告或错误标志,它会检测指针被隐式转换为的所有这些行void *?
在 C++ 中,我可以编写一个带有带std::string参数的构造函数的类。由于隐式转换,这将允许我从std::string或构造此类的实例char *。
有没有理由同时拥有std::string构造函数和char *构造函数?
class MyStringClass {
public:
MyStringClass( const std::string &str ); // char *'s could implicitly use this constructor
MyStringClass( const char * str ); // would this ever be necessary?
};
Run Code Online (Sandbox Code Playgroud)
这个问题也适用于函数参数。
void do_stuff_with_string( const std::string &str );
void do_stuff_with_string( const char * str );
Run Code Online (Sandbox Code Playgroud)
编辑:
澄清一下,我想知道更多关于性能的信息。假设这些构造函数/函数正在调用只接受char *. std::string如果我不需要,有两个单独的函数来避免构造 a是否值得?
void do_stuff_with_string( const std::string &str )
{
do_stuff_with_string( str.c_str() );
}
void …Run Code Online (Sandbox Code Playgroud) 我瞪着它并试图在SO上找到类似的问题,但没有找到任何有用的东西.所以,在这里发布我的问题.
考虑这个程序:
#include <iostream>
void foo(const std::string &) {}
int main()
{
foo(false);
}
Run Code Online (Sandbox Code Playgroud)
[Warning] converting 'false' to pointer type for argument 1 of 'std::basic_string::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' [-Wconversion-null]
为什么C++在没有显式强制转换的情 我原以为会遇到编译错误.由于以下异常显示,程序在运行时异常终止:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)
关于这种隐式转换的标准是什么?
今天我看到了一个非常奇怪的类型推导。这是代码:
unsigned int y = 15;
int k = 5;
auto t = k - y / 2;
Run Code Online (Sandbox Code Playgroud)
由于kis int,我认为该类型也t应该是int。但令我惊讶的是,它的类型是unsigned int. 我找不到为什么类型被推断为unsigned int. 知道为什么吗?
我遇到了一些模板内容的问题,我已将范围缩小到以下示例 (C++17):
template <typename T> struct item {
operator item<const T> () const { return item<const T>(); }
};
void conversionToConstRefWorks (const item<const int> &) { }
template <typename T>
void butNotWhenTemplated (const item<const T> &) { }
int main () {
item<int> i;
item<const int> ci;
// these all compile fine:
conversionToConstRefWorks(ci);
conversionToConstRefWorks(i);
butNotWhenTemplated(ci);
// but this one fails:
butNotWhenTemplated(i);
}
Run Code Online (Sandbox Code Playgroud)
在那个例子中:
item<T>有一个隐式转换运算符 to item<const T>,和conversionToConstRefWorks(),但是butNotWhenTemplated(),其中 anitem<const int> …