参数包扩展由VS2015编译器反转.
我有以下代码:
#include <iostream>
#include <vector>
template <typename... T>
void f_Swallow(T &&...)
{
}
template <typename... T>
std::vector<int> f(T ...arg)
{
std::vector<int> result;
f_Swallow
(
[&]()
{
result.push_back(arg);
return true;
}
()...
) ;
return result;
}
using namespace std;
int main()
{
auto vec = f(1,2,3,4);
for (size_t i = 0; i < vec.size(); ++i)
cout << vec[i] << endl;
}
Run Code Online (Sandbox Code Playgroud)
当我在XCode(clang-700.1.81)中运行此代码时,我得到以下结果:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
但VS2015中运行的相同代码会产生此输出:
4
3
2
1
Run Code Online (Sandbox Code Playgroud)
为什么参数包根据编译器的不同而不同地扩展?有没有办法在不检查平台和编译器版本的情况下修复它?标准不保证扩展订单的任何内容吗?
考虑以下代码:
struct foo{};
int main() {
foo::foo a;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个格式正确,foo
通过[class]/2中的规则声明一个类型的变量(N4140,强调我的):
一个类名被插入在其中后立即宣布的范围类的名字能够被看见. 的类名也被插入到类本身的范围 ; 这被称为注入类名.出于访问检查的目的,inject-class-name被视为公共成员名称.
clang 3.6.0
同意我的意见,编译上面的代码没有适用的警告-Wall -pedantic
.
gcc 5.2.0
不同意,提供以下错误消息:
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'foo::foo' names the constructor, not the type
foo::foo a;
Run Code Online (Sandbox Code Playgroud)
无论注入的类名称的嵌套有多深,例如,上述情况都适用foo::foo::foo::foo
.
是否存在强制该构造在该上下文中被解释为构造函数的规则,或者这是一个gcc
错误?或者我是否错误地解释标准报价?
可能重复:
单个列表中的对
我有一个小整数列表,说:
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
我希望收集顺序对并返回一个包含从这些对创建的元组的新列表,即:
[(1, 2), (3, 4), (5, 6)]
Run Code Online (Sandbox Code Playgroud)
我知道必须有一个非常简单的方法来做到这一点,但不能完全解决它.
谢谢
考虑 Linux 系统上的以下两个文件:
使用消息.cpp
#include <iostream>
extern const char* message;
void print_message();
int main() {
std::cout << message << '\n';
print_message();
}
Run Code Online (Sandbox Code Playgroud)
libmessage.cpp
#include <iostream>
const char* message = "Meow!"; // 1. absolute address of string literal
// needs runtime relocation in a .so
void print_message() {
std::cout << message << '\n';
}
Run Code Online (Sandbox Code Playgroud)
我们可以将use_message.cpp编译为目标文件,将libmessage.cpp编译为共享库,并将它们链接在一起,如下所示:
$ g++ use_message.cpp -c -pie -o use_message.o
$ g++ libmessage.cpp -fPIC -shared -o libmessage.so
$ g++ use_message.o libmessage.so -o use_message
Run Code Online (Sandbox Code Playgroud)
的定义message …
考虑以下代码:
using type = long;
namespace n {
using type = long;
}
using namespace n;
int main() {
type t;
}
Run Code Online (Sandbox Code Playgroud)
这在Clang 3.7和GCC 5.3上完全编译,但MSVC 19*给出以下错误消息:
main.cpp(9): error C2872: 'type': ambiguous symbol
main.cpp(1): note: could be 'long type'
main.cpp(4): note: or 'n::type'
Run Code Online (Sandbox Code Playgroud)
这段代码是否格式良好?标准的哪一部分注意到在模糊检查之前是否解决了别名?
请注意,如果更改其中一个别名,Clang和GCC都会向MSVC提供类似的错误.
我完全清楚这个名字的合格性如何解决这种模糊性,我只是对标准对此有何看法感兴趣.
* - 只需粘贴代码并在该链接上运行它,我不知道是否有永久链接的在线MSVC编译器
我遇到了一些有趣的可变参数模板函数行为.任何人都能指出标准中定义此规则的相关规则吗?
GCC,ICC和MSVC成功编译了以下代码(Clang没有,但我知道这是由于编译器错误).
template<class A, class... Bs, class C>
void foo(A, Bs..., C) { }
int main()
{
foo<int, int, int, int>(1, 2, 3, 4, 5);
}
Run Code Online (Sandbox Code Playgroud)
在此呼吁foo
,提供了模板参数为A
和Bs
,然后C
推断为int
.
但是,如果我们只是翻转最后两个模板参数:
template<class A, class C, class... Bs>
void foo(A, Bs..., C) { }
Run Code Online (Sandbox Code Playgroud)
main.cpp: In function 'int main()':
main.cpp:8:42: error: no matching function for call to 'foo(int, int, int, int, int)'
foo<int, int, …
Run Code Online (Sandbox Code Playgroud) 我正在python中编写跨平台文件资源管理器.我试图将路径中的任何反斜杠转换为正斜杠,以便以一种格式处理所有路径.
我已经尝试过不仅使用string.replace(str,'\\','/'),而且还手动创建一个方法来搜索字符串并替换实例,并且两者都无法正常工作,作为路径名如:
\dir\anotherdir\foodir\more
Run Code Online (Sandbox Code Playgroud)
更改为:
/dir/anotherdir\x0oodir/more
Run Code Online (Sandbox Code Playgroud)
我假设这与Python如何表示转义字符或类似的东西有关.我该如何防止这种情况发生?
功能模板与转发参考参数之间有什么区别
template<typename T>
void Universal_func(T && a)
{
}
Run Code Online (Sandbox Code Playgroud)
和缩写功能模板?
void auto_fun(auto && a)
{
}
Run Code Online (Sandbox Code Playgroud)
我可以代替Universal_func
用auto_fun
?是Universal_func
一个的auto_fun
或者是他们平等的吗?
我测试了下面的程序.似乎两者都是一样的.
template<typename T>
void Universal_func(T && a)
{
}
void auto_fun(auto && a)
{
}
int main()
{
int i;
const int const_i = 0;
const int const_ref =const_i;
//forwarding reference template function example
Universal_func(1); //call void Universal_func<int>(int&&)
Universal_func(i);//call void Universal_func<int&>(int&):
Universal_func(const_i); //call void Universal_func<int const&>(int const&)
Universal_func(const_ref);//call void Universal_func<int const&>(int const&) …
Run Code Online (Sandbox Code Playgroud) c++ templates type-deduction template-argument-deduction c++14
我正在使用Tkinter编写Python应用程序来管理我的GUI.
我有一个文本输入框,我试图实现一个自动完成功能,它将绑定到Tab键.
我已将Tab键绑定到我的输入框,但是当我按Tab键时,程序会尝试在GUI元素之间循环.
如何覆盖此默认行为,以便GUI仅在按键上执行指定的命令?
谢谢
我在Haskell中编写的程序有一个我定义类型的列表实例:
type Locals = [(String, Float)]
Run Code Online (Sandbox Code Playgroud)
我试图通过接收字符串并更新相应的浮点值来更新此列表,但String可能不在列表中.
由于Haskell列表是不可变的,我决定最简单的方法是执行此操作(在伪代码中):
if a tuple containing the string exists:
delete it
add the correct data
Run Code Online (Sandbox Code Playgroud)
我想知道:
a)如果有更简单的方法可以做到这一点
b)如果没有,我将如何删除正确的元素
谢谢
c++ ×6
python ×3
templates ×3
c++11 ×2
c++14 ×2
abi ×1
clang ×1
elf ×1
gcc ×1
haskell ×1
list ×1
name-lookup ×1
parameters ×1
relocation ×1
template-argument-deduction ×1
tkinter ×1
typedef ×1