小编Tar*_*ama的帖子

为什么参数包扩展在不同的C++编译器中的工作方式不同?

参数包扩展由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)

为什么参数包根据编译器的不同而不同地扩展?有没有办法在不检查平台和编译器版本的情况下修复它?标准不保证扩展订单的任何内容吗?

c++ parameters templates c++11

38
推荐指数
2
解决办法
1463
查看次数

注入的类名编译器差异

考虑以下代码:

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错误?或者我是否错误地解释标准报价?

c++ gcc clang language-lawyer c++14

22
推荐指数
2
解决办法
3741
查看次数

将列表中的每对元素收集到Python中的元组中

可能重复:
单个列表中的对

我有一个小整数列表,说:

[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

我希望收集顺序对并返回一个包含从这些对创建的元组的新列表,即:

[(1, 2), (3, 4), (5, 6)]
Run Code Online (Sandbox Code Playgroud)

我知道必须有一个非常简单的方法来做到这一点,但不能完全解决它.

谢谢

python

20
推荐指数
3
解决办法
2万
查看次数

ELF 重定位的应用顺序在哪里指定?

考虑 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 …

c++ elf abi relocation dynamic-linking

14
推荐指数
1
解决办法
1656
查看次数

编译器差异:别名解析和名称查找之间的交互

考虑以下代码:

using type = long;

namespace n {
  using type = long;
}

using namespace n;
int main() {
  type t;
}
Run Code Online (Sandbox Code Playgroud)

这在Clang 3.7GCC 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编译器

c++ typedef language-lawyer name-lookup c++11

10
推荐指数
1
解决办法
135
查看次数

令人困惑的非尾随参数包行为

我遇到了一些有趣的可变参数模板函数行为.任何人都能指出标准中定义此规则的相关规则吗?

GCC,ICCMSVC成功编译了以下代码(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,提供了模板参数为ABs,然后C推断为int.

但是,如果我们只是翻转最后两个模板参数:

template<class A, class C, class... Bs>
void foo(A, Bs..., C) { }
Run Code Online (Sandbox Code Playgroud)

然后所有 三个 编译器都会抛出错误.这是GCC的一个:

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)

c++ templates language-lawyer

10
推荐指数
1
解决办法
142
查看次数

在Python中用反斜杠替换反斜杠

我正在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如何表示转义字符或类似的东西有关.我该如何防止这种情况发生?

python

8
推荐指数
3
解决办法
2万
查看次数

缩写功能模板与具有转发参考参数的功能模板

功能模板与转发参考参数之间有什么区别

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_funcauto_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

8
推荐指数
1
解决办法
451
查看次数

覆盖Python Tkinter中的默认选项卡行为

我正在使用Tkinter编写Python应用程序来管理我的GUI.

我有一个文本输入框,我试图实现一个自动完成功能,它将绑定到Tab键.

我已将Tab键绑定到我的输入框,但是当我按Tab键时,程序会尝试在GUI元素之间循环.

如何覆盖此默认行为,以便GUI仅在按键上执行指定的命令?

谢谢

python user-interface tkinter

5
推荐指数
1
解决办法
1175
查看次数

在Haskell中更新列表的元组元素

我在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)如果没有,我将如何删除正确的元素

谢谢

haskell list

5
推荐指数
1
解决办法
2690
查看次数