小编GSi*_*GSi的帖子

Initializer_list作为非模板上下文中的数组引用参数的参数

我的问题涉及这个非常简单和简短的代码,其中在接受数组引用参数的两个非模板函数之间尝试重载解析.该问题已在其他地方发布,但在模板演绎上下文中.这是代码:

#include <iostream>

void foo ( const int (&x) [3] ) { std::cout << "3\n"; }
void foo ( const int (&x) [2] ) { std::cout << "2\n"; }

int main()
{
    foo({1,2,3});
}
Run Code Online (Sandbox Code Playgroud)

g ++ 4.8.3编译这段代码选择第一个函数(我想)只有可行,而clang 3.4不编译它,说对foo的调用是不明确的(为什么?).

哪个编译器做对了?

clang甚至不会编译代码,甚至删除第二个重载:似乎初始化数组引用似乎不接受initializer_list.

这辆车有问题吗?

c++ initializer-list overload-resolution c++11

13
推荐指数
1
解决办法
357
查看次数

在GNU编译器中,typedef的名称查找是错误的吗?

以下代码

#include <iostream>

typedef double A; // a global typedef

template <class Z> struct B // a template class...
{
    A i{22.2}; // global typedef is in scope
    typedef int A; // now  a local typedef with the same name is introduced
    A b{24};  // now the local typedef is in scope
    Z c{36}; // a simple member of the template type
};

template <class Z> struct C : B<Z> // a template struct inheriting B
{
    A a;  // …
Run Code Online (Sandbox Code Playgroud)

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

7
推荐指数
1
解决办法
184
查看次数

模板指针参数包

为什么带有模板参数包指针的模板函数无法使用相同指针的偏移量进行实例化?

我的意思是:给出这个简短的代码为什么我必须注释掉最后两行?

template <int * ... pt> void f() {}

int n[] = {1, 2, 3};
int m = n[1];

int main()
{
    f<n>();  // this is accepted
    f<n, &m>();  // this is accepted too
    //f<n, n+1>(); // this is not.
    //f<n, &n[1]>(); // this isn't accepted neither
}
Run Code Online (Sandbox Code Playgroud)

并不n+1代表相同的地址&m?或者链接有什么不同?还有什么?

c++ templates language-lawyer variadic-templates

6
推荐指数
1
解决办法
141
查看次数

在可变参数函数模板中没有推导出上下文

如我所知,以下代码应该是"不推断上下文"(或不是?)

template <class... X, class Y>
void f(X... args, Y y)
{

}

int main()
{
    f(12);
    f<int, int, int>(1, 2, 3, 4);
}
Run Code Online (Sandbox Code Playgroud)

但G ++ 4.9编译它的两个实例fmain...谁能解释一下吗?

c++ variadic-templates c++11 template-argument-deduction

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

std :: istream类型是EqualityComparable吗?

我的问题会有一个布尔答案:是或否.无论它是什么,有人可以解释GNU-g ++ 4.9.2和clang 3.5如何编译以下代码,而GNU-g ++ 5.1.1不再接受它,声称没有匹配operator==

对于最后一个编译器,如何改变它,以便获得相同的结果,即operator>>能够以这种简单的方式区分它是由标准输入流还是由其他东西调用?

  # include <iostream>
  # include <fstream>

  struct S {};

  std::istream& operator >> (std::istream& i, S& s)
   {
    if(i == std::cin) std::clog << "this is standard input\n";
    else std::clog << "this is some other input stream\n";

    return i;
    }

  int main(int narg, const char ** args)
   {
    S s;
    std :: cin >> s;
    std::ifstream inp(args[1]);
    inp >> s;
    }
  // to be executed with the name of an existing …
Run Code Online (Sandbox Code Playgroud)

c++ equality istream c++11

4
推荐指数
2
解决办法
691
查看次数

删除的副本分配检测到虚假错误?

我有以下简单的结构

  struct crazy
    {
     const int i = 10;
     };
Run Code Online (Sandbox Code Playgroud)

这显然是不可复制的.确实,如果我有两个这种类型的对象,比如object1和object2,并尝试一个类似的语句

  object1 = object2;
Run Code Online (Sandbox Code Playgroud)

我处理的两个编译器,即clang 3.4.2和gcc 4.8.3(好吧,它们可能已经过时了......),抱怨并拒绝使用合理和类似诊断的代码.

但是为什么在那种情况下,gcc在const成员i的初始化中也检测到错误?无论我用"constexpr"改变"const",都会检测到这样的错误.

c++ gcc

3
推荐指数
1
解决办法
251
查看次数

案例标签不需要空格?

可能这是一个愚蠢的问题,但有没有人知道case关键字与其在switch语句中的常量表达式之间是否存在必要的空格?

标准似乎没有说什么......

请考虑以下代码:

switch(int_expression)
{
    case1:     /*anything*/  // no space before 1
    caseZERO:  /*anything*/  // no space before ZERO
                             // ZERO being defined as 0
                             // by the pre-processor
}
Run Code Online (Sandbox Code Playgroud)

我的参考编译器都接受这个代码,一旦运行它就可以很好地工作.预处理器如何识别必须替换ZERO?

另请注意,如果控件表达式是char类型而不是int类型,则还会编译以下内容,但这次它不再有效

switch(char_expression)
{
    case'a':   /* anything */  // NO SPACES embedded
    caseZERO:  /* anything */  // NO SPACES again
                               // ZERO being defined as '0'
                               // by the pre-processor
}
Run Code Online (Sandbox Code Playgroud)

这个编译,但即使char_expression是'0' 的值,也没有caseZERO执行任何语句.

有谁能解释一下?

c c++ switch-statement

2
推荐指数
1
解决办法
322
查看次数