是否可以接收具有保证返回类型R但参数数量可变的函数类型作为内联 lambda 参数?为简单起见,请考虑以下事项:
inline fun <R> Boolean?.tfn(tru:()->R, fls:()->R, nul:()->R) =
if (this == null) nul() else if (this) tru() else fls()
Run Code Online (Sandbox Code Playgroud)
假设我还有其他内联函数,它们也采用可能为or或类型的lambda 参数,我希望将其参数传递给该函数。它们都将提供 an但必须调用它们才能获取,而不知道该函数中参数的数量/类型/值。有没有办法将上面的函数一般修改为:(A)->R(A,B)->R(A,B,C)->RRR
R捕获返回其输入的任何 lambda 参数的一般情况我已经阅读了有关内联两种含义的现有答案,但我仍然感到困惑。
假设我们有以下头文件:
// myclass.h
#ifndef INCLUDED_MYCLASS
#define INCLUDED_MYCLASS
class MyClass
{
public:
void foo(); // declaration
};
inline void MyClass::foo()
{
// definition
}
#endif
Run Code Online (Sandbox Code Playgroud)
为什么void foo()在文件中的类之外定义的 which 必须使用 显式定义inline?
我想在预购模型和产品模型之间创建内联表单集。场景是,当用户决定创建预购时,他将能够选择一种或多种产品。另一方面,一种产品可能会出现在一个或多个预购订单中。考虑到这一点,我创建了多对多关系。
模型.py
class Preorder(models.Model):
client = models.ForeignKey(Client,verbose_name=u'Client')
invoice_date = models.DateField("Invoice date",null=True, blank=True, default=datetime.date.today)
preorder_has_products = models.ManyToManyField(Product, blank=True)
def get_absolute_url(self):
return reverse('preorder_edit', kwargs={'pk': self.pk})
class Product(models.Model):
name = models.CharField("Name",max_length=200)
price = models.DecimalField("Price", max_digits=7, decimal_places=2, default=0)
barcode = models.CharField(max_length=16, blank=True, default="")
eopyy = models.CharField("Code eoppy",max_length=10, blank=True, default="")
fpa = models.ForeignKey(FPA, null=True, blank=True, verbose_name=u'Fpa Scale')
Run Code Online (Sandbox Code Playgroud)
表格.py
class PreorderForm(ModelForm):
class Meta:
model = Preorder
exclude = ('client','preorder_has_products',)
def __init__(self, *args, **kwargs):
super(PreorderForm, self).__init__(*args,**kwargs)
self.fields['invoice_date'].widget = MyDateInput(attrs={'class':'date'})
class ProductForm(ModelForm):
#name = ModelChoiceField(required=True,queryset=Product.objects.all(),widget=autocomplete.ModelSelect2(url='name-autocomplete')) …Run Code Online (Sandbox Code Playgroud) 有什么方法可以指示 go 编译器不内联吗?
$ cat primes.go
package main
import ("fmt")
func isPrime(p int) bool {
for i := 2; i < p; i += 1 {
for j := 2; j < p; j += 1 {
if i*j == p {
return false
}
}
}
return true
}
func main() {
for p := 2; p < 100; p+= 1 {
if isPrime(p) {
fmt.Println(p)
}
}
}
$ go build primes.go
$ objdump -D -S …Run Code Online (Sandbox Code Playgroud) 我希望std::map标头中的 const 作为将在其他 cpp-s 中使用的全局常量。所以我将其声明为:
// header.h
const inline std::map<int, int> GlobalMap = { {1, 2}, {3, 4} };
Run Code Online (Sandbox Code Playgroud)
但是,如果我在多个 cpp-s 中包含此标头,则在退出时会发生堆损坏,因为为同一内存地址运行多个析构函数。
我一直认为内联 const 是全局非文字常量的灵丹妙药。我已将 global std::string-s 声明为const内联,并且效果很好。
所以我的问题是:
const内联不是很容易出错吗?const std::map在 C++17 中正确声明全局?我怎样才能确保只创建一个全局对象?编辑:我可以在 Visual Studio 2017 中的以下项目中重现它(/std:c++17,调试 x86)
文件_1.h:
#pragma once
#include <map>
const inline std::map<int, double> GlobalMap = {{1, 1.5}, {2, 2.5}, {3, 3.5}};
void f1();
Run Code Online (Sandbox Code Playgroud)
文件_1.cpp:
#include "file_1.h"
void f1()
{
(void)GlobalMap;
}
Run Code Online (Sandbox Code Playgroud)
主要.cpp:
#include "file_1.h"
int …Run Code Online (Sandbox Code Playgroud) 我正在阅读kotlin文档,但无法理解一件事。
我们可以使用noinline修饰符来标记 lambda 不应该是inline. 但当我们真正需要它时,真实情况又是怎样呢?
我明白是noinline什么,但不明白为什么。
inline在说明符的 cppreference 页面上,它说:
inline当在函数的decl-specifier-seq中使用说明符时,该说明符将该函数声明为内联函数。
内联函数具有以下属性:程序中
可以有多个内联函数的定义,只要每个定义出现在不同的翻译单元中并且所有定义都相同。
...
然后,稍微低一点,它说,
该关键字的最初目的
inline是向优化器指示函数的内联替换优于函数调用。...
除了这一行之外,没有任何关于 的使用的参考inline。后一个含义inline在 C++ 标准中仍然有效吗?或者后一种形式是否比前一种形式被弃用?
如果后一种形式仍然有效,是否值得在现代编译器中使用它?我听说,即使是编译器做出内联决定,但使用inline关键字会稍微推动它。这是真的?对于这种情况,我应该inline在程序中使用关键字吗?
可能重复:
何时使用内联函数和何时不使用它?
我已经看到许多源代码使用与inline指令不同的语法.
namespace Foo
{
class Bar
{
public:
// 1 - inline on the declaration + implementation
inline int sum1(int a, int b) { return a + b; }
// 2 - inline on template declaration + implementation
template <typename T>
inline T sum2(T a, T b) { return a + b; }
// 3 - Nothing special on the declaration...
int sum3(int a, int b);
};
// 3 - But the inline directive goes after …Run Code Online (Sandbox Code Playgroud) inline ×10
c++ ×5
kotlin ×2
bottom-sheet ×1
c++17 ×1
coding-style ×1
constants ×1
dart ×1
datepicker ×1
django ×1
flutter ×1
function ×1
generics ×1
go ×1
header-files ×1
lambda ×1
member ×1
stdmap ×1