试图编译:
class AnonymousClass
{
public:
AnonymousClass(int x)
{
}
};
int main()
{
int x;
AnonymousClass(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从MSVC生成错误:
foo.cpp(13) : error C2371: 'x' : redefinition; different basic types
foo.cpp(12) : see declaration of 'x'
foo.cpp(13) : error C2512: 'AnonymousClass' : no appropriate default constructor available
Run Code Online (Sandbox Code Playgroud)
g ++的错误消息类似:
foo.cpp: In function ‘int main()’:
foo.cpp:13: error: conflicting declaration ‘AnonymousClass x’
foo.cpp:12: error: ‘x’ has a previous declaration as ‘int x’
foo.cpp:12: warning: unused variable ‘x’
Run Code Online (Sandbox Code Playgroud)
通过给AnonymousClass对象一个明确的名称可以很容易地修复它,但是这里发生了什么以及为什么?我认为这是更多的声明语法怪异(如comp.lang.C++ FAQ的Q10.2 …
void *
memchr(const void *s, int c, size_t n)
{
if (n != 0) {
const unsigned char *p = s;
do {
if (*p++ == (unsigned char)c)
return ((void *)(p - 1));
} while (--n != 0);
}
return (NULL);
}
Run Code Online (Sandbox Code Playgroud)
对我来说似乎不必要的复杂; 初步n != 0检查do- while只是为了避免p声明似乎完全没有意义.但是,我特别感兴趣的是循环体的作用:
if (*p++ == (unsigned char)c)
return ((void *)(p - 1));
Run Code Online (Sandbox Code Playgroud)
而不是更直接:
if (*p == (unsigned char)c)
return ((void *) p);
++p;
Run Code Online (Sandbox Code Playgroud)
使用条件内联后增量是否对某些编译器/平台有一些优化优势?
我正在实现一个容器,如:
template<typename T>
class Container
{
public:
using value_type = T;
...
};
Run Code Online (Sandbox Code Playgroud)
有没有办法得到一个好办法const value_type从const Container?
背景:
我已经通过嵌套模板类实现了迭代器类型:
template<typename Container, typename Value>
class iterator_base
{
public:
...
Value& operator*() const;
private:
Container* c;
};
using iterator = iterator_base<Container, value_type>;
using const_iterator = iterator_base<const Container, const value_type>;
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但第二个模板参数iterator_base感觉多余.
示例代码
Map<String,String> gg={'gg':'abc','kk':'kojk'};
Future<void> secondAsync() async {
await Future.delayed(const Duration(seconds: 2));
print("Second!");
gg.forEach((key,value) async{await Future.delayed(const Duration(seconds: 5));
print("Third!");
});
}
Future<void> thirdAsync() async {
await Future<String>.delayed(const Duration(seconds: 2));
print('third');
}
void main() async {
secondAsync().then((_){thirdAsync();});
}
Run Code Online (Sandbox Code Playgroud)
输出
Second!
third
Third!
Third!
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我想用来等待地图的 foreach 循环完成然后我想打印third
预期的输出
Second!
Third!
Third!
third
Run Code Online (Sandbox Code Playgroud) 大家好,我是 Isar Flutter 的新手。我想知道有什么方法可以将现有的 JSON 文件导入到 Isar 中吗?
我尝试在互联网上搜索此内容,但找不到太多内容。
下面是我的 Json 文件的结构。
{
"food": [
{
"id": "0001",
"name": "Cake",
"description": [
{
"type": "Chocolate",
"quantity": 1
},
{
"type": "fruits",
"quantity": 3
},
{
"type": "Corn",
"quantity": 6
}
]
},
{
"id": "0002",
"name": "Raised",
"description": [
{
"type": "Grape",
"quantity": 6
},
{
"type": "Wheat",
"quantity": 2
}
]
}
],
"instruction": [
{
"category": "instruction_1002",
"content": "abc1234"
},
{
"category": "instruction_1003",
"content": "def56789"
}
]
}
Run Code Online (Sandbox Code Playgroud) 如果我编写一个名为的脚本git-foo,并将其放在我的某个地方PATH,那么我可以运行git foo执行该脚本并运行git foo --option argument以将命令行选项和参数传递给我的脚本.
但是,在尝试打印帮助文档时,这种语法糖会崩溃.我已经完成了git-foo --help工作,但如果我运行git foo --help,git不会转发--help到我的脚本,而是尝试打开一个man页面git-foo.我不想要这个.有没有办法抑制自定义命令的这种行为?(理想情况下,作为脚本作者,但我愿意以一种方式来解决这个问题.)
我怀疑我希望生成一个man页面,但这似乎相当繁重:那么脚本需要与一个额外的文件打包,而额外的文件需要安装在其他地方,然后会有更多卸载时要撤消的工作.
在C++中,当使用initializer_list语法初始化对象时,当没有其他列表初始化规则适用时,对象的常规构造函数也参与重载解析.据我所知,以下代码调用X :: X(int)
class X { int a_; X(int a):a_(a) {} );
void foo() {
X bar{3};
}
Run Code Online (Sandbox Code Playgroud)
但我不明白,为什么常规构造函数也被考虑在initializer_lists的上下文中.我觉得很多程序员现在编写X {3}来调用构造函数而不是X(3)来调用construcor.我根本不喜欢这种风格,因为它让我觉得这个对象没有常规的构造函数.
initializer_list语法也可用于调用常规构造函数的原因是什么?是否有理由比常规构造函数调用更喜欢这种语法?
我想Map<String,int>按值对 a 进行排序。
{'201': 4, '2017CS197': 2, '2017CS300': 6, '202': 4, '205': 3, '206': 3, '207': 5}
Run Code Online (Sandbox Code Playgroud)
这是我的地图。
在我想得到结果为 {'2017CS300': 6,'207':5,'202':4,'201':4,'206':3,'205':3,'2017CS197':2}
谢谢你!
我正在开发一个Flutter Module,多个Flutter Plugins。他们之间的关系是:module取决于一切plugins。所有插件均未发布到 pub 并使用 git 依赖项。
我现在需要添加analysis_options.yaml到每个plugin,配置是一样的。所以我有一个想法,所有插件都可以共享吗same analysis_options.yaml?我还没有找到方法。
flutter analyze不能指定目录,默认在当前项目中执行,analysis_options.yaml可以应用规则。
dart analyze可以指定目录或文件,但不能应用analysis_options.yaml规则。如果我想指定目录或文件扫描,还想指定自定义规则,我该怎么做?
我想创建一个如下所示的集合,但是当我运行 flutter pub run build_runner build --delete-conflicting-outputs 时,出现以下错误。
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:isar/isar.dart';
part 'model.freezed.dart';
part 'model.g.dart';
@freezed
@Collection(ignore: {'copyWith'}, inheritance: false)
class Model with _$Model {
const factory Model({
required String address,
}) = _Model;
Id get id => Isar.autoIncrement;
const Model._();
}
Run Code Online (Sandbox Code Playgroud)
错误:
Constructor parameter does not match a property.
|
| required String address,
|
Run Code Online (Sandbox Code Playgroud)
尽管有一个简单的集合,但我遇到了错误并陷入困境。我该如何解决这个错误?
此外,在生成冻结文件时,不会创建 的.g.文件。Isar