为什么这有效:
type Money =
{ Amount : decimal } with
member inline m.gotMoney : bool =
m.Amount > 0M
Run Code Online (Sandbox Code Playgroud)
但这并不
type MoneyUOM<[<Measure>]'currency> =
{ Amount : decimal<'currency> } with
member inline m.gotMoney : bool =
m.Amount > 0M<_>
Run Code Online (Sandbox Code Playgroud)
相反,我得到error FS0339: The signature and implementation are not compatible because the type parameter in the class/signature has a different compile-time requirement to the one in the member/implementation
我有一个泛型函数,需要实例化其泛型参数的对象并将其传递给某个接口的实例。
据我所知,实例化通用对象的唯一方法是使函数内联并具体化该类型参数。但我不想公开该接口的实现。
问题是内联函数不能使用内部类。
我基本上想要的是这样的:
/* The interface I want to expose */
interface Params<T> {
val doc: T
}
/* The implementation I do not want to expose */
internal class ParamsImpl<T> (override val doc: T) : Params<T>
/* The function */
inline fun <reified T> doSomething(init: Params<T>.() -> Unit) {
val doc= T::class.java.newInstance()
val params = ParamsImpl(doc) // Can't compile since ParamsImpl is internal
params.init()
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码
#include <iostream>
#include <functional>
using namespace std;
inline void readandrun(function<void(int)> callback) {
int i;
i = 1;
callback(i);
}
int main(int argc, char *argv[])
{
#ifdef LAMBDA
readandrun([](int i){ printf("the read number is: %d\n",i);});
#else
int i;
i = 1;
printf("the read number is: %d\n",i);
#endif
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译
g++ -DLAMBDA -O2 -std=c++17 -S test.cpp -o test_long.S
Run Code Online (Sandbox Code Playgroud)
产生涉及跳转的代码,而
g++ -O2 -std=c++17 -S test.cpp -o test_short.S
Run Code Online (Sandbox Code Playgroud)
才不是。哪一种是有道理的,但是否可以告诉编译器内联 lambda 参数,因为它在编译时是已知的?我愿意切换编译器,但为了完整性:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version …Run Code Online (Sandbox Code Playgroud) 我是 React 新手,如果有人能提供帮助,我将不胜感激......
我使用“create-react-app”设置环境,所有图像文件都位于文件夹img下的src文件夹中。其中一个组件动态设置背景图像。
这工作正常:
<div style={{ background: `url(${require("../img/file-name.jpg")}) no-repeat`}}/>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用变量作为文件名(如下所示)时,出现错误:错误:找不到模块“../img/file-name.jpg”
let imageUrl = '../img/' + filenames[0];
...
<div style={{ background: `url(${require(imageUrl)}) no-repeat`}}/>
Run Code Online (Sandbox Code Playgroud)
我也尝试了类似下面的测试,但我得到了同样的错误。
let imageUrl = '../img/file-name.jpg';
...
<div style={{ background: `url(${require(imageUrl)}) no-repeat`}}/>
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供帮助,我将不胜感激!谢谢你!
下一个代码在 CLang/GCC 中编译,并成功内联 lambda:
#include <iostream>
int main() {
int x = 0;
auto f = [&]() __attribute__((always_inline)) {
++x;
};
f();
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)
但是与__forceinline最新的 MSVC(2019 v16.8.3)中的类似代码无法编译,尽管已宣布在 v16.7 中实现:
#include <iostream>
int main() {
int x = 0;
auto f = [&]() __forceinline {
++x;
};
f();
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)
抛出编译错误0305.cpp(5): error C3260: 'type': skipping unexpected token(s) before lambda body。
它真的还没有实现还是我用__forceinline错了地方?有没有其他方法可以在 MSVC 中强制内联 lambda?
在所有流行的编译器(例如 CLang/GCC/MSVC)中是否有任何方法不编译代码(并抛出编译错误),以防在某些地方使用给定的 lambda …
在将旧的 Turbo Pascal 单位转换为现代 Object Pascal 时,我遇到了以下问题:
function Less (var a, b; Relation : POINTER) : boolean;
inline($5B/$59/$0E/$E8/$00/$00/$58/$05/$08/$00/$50/$51/$53/$CB);
Run Code Online (Sandbox Code Playgroud)
代码应该调用 an external function {$F+} function VariableLess(var a, b : Index) : boolean; {$F-},收集结果并将其传递给调用函数。该函数用于为无类型数据提供二叉树的单元中
procedure InsVarBBTree(var B: BBTree; var E; S: word; A: pointer; var ok: boolean);
{ puts variable E of size S into tree B. The order relation address is A. }
Run Code Online (Sandbox Code Playgroud)
因此,单元本身不能提供比较功能,即定义有效载荷的单元的工作。
使用在线反汇编器,我发现这对应于:
{$ASMMODE intel}
function Less (var a, b; Relation : POINTER) : boolean; assembler; …Run Code Online (Sandbox Code Playgroud) 请注意:这只是出于好奇而提出的问题,与编写更好的多线程代码无关。当然,我不会也不会在实际项目中编写这样的代码。
添加关键字时可能会发生内联替换inline。所以我很好奇。
假设我们有这样的代码:
static bool done = false;
inline void setDone(bool newState) {
done = newState;
}
inline bool getDone() {
return done;
}
void someWorkerThreadJob() {
// Accessing without any lock/atomic/volatile
while (getDone() == false) {
}
}
Run Code Online (Sandbox Code Playgroud)
可以someWorkerThreadJob()像下面这样编译并进入无限循环吗?
void someThreadJob() {
while (done == false) {
}
}
Run Code Online (Sandbox Code Playgroud)
这也将我引向了下一个问题。类中的getter和setter怎么样?在类中定义的成员函数是隐式的inline,所以我认为可能会发生内联替换,因此同样的问题。这样对吗?
我读到的所有关于类型具体化的教程都说我们在使用“具体化”时需要使用“内联”,但没有一个解释原因。
假设我有一个函数:
inline fun <reified T> doSomething(value: T) {
println("Doing something with type: ${T::class.simpleName}")
}
Run Code Online (Sandbox Code Playgroud)
据我了解,使用“具体化”可以防止类型擦除。那么为什么我们不能在普通的非内联函数中使用它呢?使用内联将使编译器在调用站点复制上述函数的主体。但为什么我们需要这样的事情发生呢?
generics inline type-erasure kotlin kotlin-reified-type-parameters
今天我编写了一些C代码来使用带有自定义比较器功能的快速排序对结构数组进行排序,以确定它们的排序.
起初我用调用比特函数编写它,硬编码到quicksort函数中.然后我想也许将该函数作为参数传递给通用的快速排序函数会更好.
在我的原始代码中,我已经声明了比较器功能inline.在我的新代码中,我保留了inline声明,即使这对我来说没有多大意义,因为函数是作为参数传递的.但是,编译器没有抱怨!
我的问题是:inline声明在这里有任何影响,还是只是对编译器的建议被忽略了?
原始代码:
typedef struct _CGRect {
CGPoint origin;
CGSize size;
} CGRect;
typedef enum _NSComparisonResult {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
} NSComparisonResult;
static inline NSComparisonResult CGRectCompareRowsFirst(CGRect r1, CGRect r2)
{
if (r1.origin.y < r2.origin.y)
return NSOrderedAscending;
else if (r1.origin.y > r2.origin.y)
return NSOrderedDescending;
else
{
if (r1.origin.x < r2.origin.x)
return NSOrderedAscending;
else if (r1.origin.x > r2.origin.x)
return NSOrderedDescending;
else
return NSOrderedSame;
}
}
static void CGRectQuicksortRowsFirst(CGRect *left, CGRect *right)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个背景图片,在文件名中有一个括号:
<DIV style="BACKGROUND: url('http://site.com/image(8).png');"></DIV>
Run Code Online (Sandbox Code Playgroud)
这通常很好,并且没有混淆,因为文件名周围有引号.
然而,在IE的开发者工具中,我看到浏览器出于某种原因剥离了引号.
<DIV style="BACKGROUND: url(http://site.com/image(8).png);"></DIV>
Run Code Online (Sandbox Code Playgroud)
它仍然有效,所以没什么大不了的.当我尝试使用jquery的clone功能时出现问题.
显然,clone()之后的输出不能识别上面的图像url在IE8中有效并将其剥离(其他浏览器,包括IE7,都可以).所以我最终得到了这个:
<DIV></DIV>
Run Code Online (Sandbox Code Playgroud)
有人知道怎么修这个东西吗?也就是说,在IE8中的url中使用括号克隆背景图像.
谢谢.
此外,作为附录我必须使用内联样式的相关元素,所以请不要建议使用单独的样式表.
Ad2:在转义时,浏览器渲染后转义的实体变为非转义状态.在页面加载后再次进行克隆,并且要克隆的选定对象中包含未转义的字符.
inline ×10
c++ ×3
generics ×2
kotlin ×2
lambda ×2
assembly ×1
background ×1
c ×1
f# ×1
f#-4.0 ×1
function ×1
jquery ×1
kotlin-reified-type-parameters ×1
parameters ×1
pascal ×1
reactjs ×1
require ×1
turbo-pascal ×1
type-erasure ×1
visual-c++ ×1
x86-16 ×1