小编HC4*_*ica的帖子

GCC和clang(SFINAE)之间的过载分辨率行为差异

GCC接受以下代码:

template <typename T>
struct meta
{
    typedef typename T::type type;
};

struct S {};

template <typename T>
typename meta<T>::type foo(T, S);

int foo(int, int);      

int main()
{
    foo(0, 0);
}
Run Code Online (Sandbox Code Playgroud)

但是clang拒绝它并出现以下错误:

test.cpp:4:22: error: type 'int' cannot be used prior to '::' because it has no members
    typedef typename T::type type;
                     ^
test.cpp:10:10: note: in instantiation of template class 'meta<int>' requested here
typename meta<T>::type foo(T, S);
         ^
test.cpp:10:24: note: while substituting deduced template arguments into function template 'foo' [with T …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates clang overload-resolution

18
推荐指数
2
解决办法
4639
查看次数

方法声明改变了符号的含义

对于以下代码:

struct foo {};

struct A
{
    typedef foo foo_type;

    void foo();
};
Run Code Online (Sandbox Code Playgroud)

GCC给出了一个编译器错误:

test.cpp:7:14: error: declaration of 'void A::foo()' [-fpermissive]
     void foo();
              ^
test.cpp:1:8: error: changes meaning of 'foo' from 'struct foo' [-fpermissive]
 struct foo {};
        ^
Run Code Online (Sandbox Code Playgroud)

但clang接受它没有编译器错误.谁是对的?

请注意,如果删除typedef或将其更改为typedef ::foo foo_type,则gcc和clang都接受该代码.

c++ token-name-resolution

18
推荐指数
1
解决办法
4041
查看次数

C++ 11向量push_back模糊不清

请考虑以下代码:

#include <vector>

struct S { int a; double b; };

int main()
{
    std::vector<S> v;
    v.push_back({3, 4.5});
}
Run Code Online (Sandbox Code Playgroud)

g ++ 4.4抱怨对push_back()的调用不明确:

error: call of overloaded ‘push_back(<brace-enclosed initializer list>)’ is ambiguous
note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = S, _Alloc = std::allocator<S>]
note:                 void std::vector<_Tp, _Alloc>::push_back(_Tp&&) [with _Tp = S, _Alloc = std::allocator<S>]
Run Code Online (Sandbox Code Playgroud)

根据标准,这应该是模棱两可的,还是这只是g ++的一个问题?

我知道可以通过明确写出S的类型来解决它:

v.push_back(S{3, 4.5});
Run Code Online (Sandbox Code Playgroud)

但是S的类型名称可能很长,所以我宁愿不这样做......

c++ vector c++11

17
推荐指数
2
解决办法
3491
查看次数

成员函数调用decltype

以下代码:

struct A
{
    int f(int);
    auto g(int x) -> decltype(f(x));
};
Run Code Online (Sandbox Code Playgroud)

无法编译错误:

error: cannot call member function 'int B::f(int)' without object
Run Code Online (Sandbox Code Playgroud)

如果我将其更改为:

struct A
{
    int f(int);
    auto g(int x) -> decltype(this->f(x));
};
Run Code Online (Sandbox Code Playgroud)

我收到另一个错误:

error: invalid use of 'this' at top level
Run Code Online (Sandbox Code Playgroud)

这些都有什么问题?我正在使用gcc 4.6

c++ member-functions decltype c++11

17
推荐指数
4
解决办法
7422
查看次数

gcc和clang之间的重载分辨率差异涉及移动构造函数和'Derived(Base &&)'构造函数

GCC(使用4.9测试)接受以下测试用例:

struct Base {};

struct Derived : Base {
    Derived();
    explicit Derived(const Derived&);
    explicit Derived(Derived&&);
    explicit Derived(const Base&);
    Derived(Base&&);
};

Derived foo() {
  Derived result;
  return result;
}

int main() {
  Derived result = foo();
}
Run Code Online (Sandbox Code Playgroud)

Clang(使用3.5测试)拒绝它,并显示以下错误消息:

test.cpp:13:10: error: no matching constructor for initialization of 'Derived'
  return result;
         ^~~~~~
test.cpp:8:5: note: candidate constructor not viable: no known conversion from 'Derived' to 'Base &&' for 1st argument
    Derived(Base&&);
    ^
test.cpp:4:5: note: candidate constructor not viable: requires 0 arguments, but 1 was …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang overload-resolution c++11

17
推荐指数
1
解决办法
213
查看次数

多重继承和纯虚函数

以下代码:

struct interface_base
{
    virtual void foo() = 0;
};

struct interface : public interface_base
{
    virtual void bar() = 0;
};

struct implementation_base : public interface_base
{
    void foo();
};

struct implementation : public implementation_base, public interface
{   
    void bar();
};

int main()
{
    implementation x;
}
Run Code Online (Sandbox Code Playgroud)

无法编译时出现以下错误:

test.cpp: In function 'int main()':
test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation'
test.cpp:16:8: note:   because the following virtual functions are pure within 'implementation':
test.cpp:3:18: note:    virtual …
Run Code Online (Sandbox Code Playgroud)

c++ virtual multiple-inheritance diamond-problem

16
推荐指数
2
解决办法
9620
查看次数

"未定义的行为"是否延伸到编译时?

我们都听过警告,如果你在C或C++中调用未定义的行为,任何事情都可能发生.

这是否仅限于任何运行时行为,还是包括任何编译时行为?特别是,编译器在遇到调用未定义行为的构造时,允许拒绝代码(在标准中没有其他要求的情况下这样做),甚至崩溃?

c c++ runtime compile-time undefined-behavior

16
推荐指数
2
解决办法
729
查看次数

C++标准委员会"反射器"邮件列表

我不时听到提到一个名为"反射器"的邮件列表或列表,其中C++标准委员会的成员讨论与他们在C++标准化方面的工作相关的问题.

这些邮件列表是否可供公众阅读?如果是这样,有人能指点我吗?如果没有,他们被关闭的理由是什么?我认为能够阅读这些讨论非常有用 - 我认为它们将为委员会作出的各种决定背后的理由提供很好的见解.

c++ standards c++11

15
推荐指数
2
解决办法
1423
查看次数

变量函数模板,包扩展不在最后一个参数中

我想知道为什么以下代码无法编译:

struct S
{
    template <typename... T>
    S(T..., int);
};

S c{0, 0};
Run Code Online (Sandbox Code Playgroud)

此代码无法使用clang和GCC 4.8进行编译.这是clang的错误:

test.cpp:7:3: error: no matching constructor for initialization of 'S'
S c{0, 0};
  ^~~~~~~
test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    S(T..., int);
    ^
Run Code Online (Sandbox Code Playgroud)

在我看来,这应该工作,T应该被推断为一个长度为1的包.

如果标准禁止做这样的事情,有谁知道为什么?

c++ templates function-templates variadic-templates c++11

15
推荐指数
2
解决办法
4086
查看次数

使用mercurial执行空提交

使用Mercurial队列扩展,我可以使用一些提交消息进行空提交,如下所示:

hg qnew patch_name -m "message"
Run Code Online (Sandbox Code Playgroud)

没有Mercurial队列有没有办法做到这一点?我试过简单地说:

hg commit -m "message"
Run Code Online (Sandbox Code Playgroud)

但是hg只是说"没有改变"并且没有做提交,我没有看到任何"强制"选项会覆盖它.

如果您想知道我这样做的动机:我们有测试基础设施,您可以将其推送到特殊存储库,它将触发自动测试运行.您需要在最尖端提交的提交消息中放入一个特殊字符串,该消息说明要运行哪些测试.显然,当我推送到实际的存储库时,我不希望这个字符串在那里.而不是修改提交两次(一次添加特殊字符串,第二次删除它),我会发现只添加一个空提交,然后回滚它更清洁 - 我可以用mq做到这一点,但是我想找到一种没有mq的方法.

version-control mercurial commit mercurial-queue

15
推荐指数
4
解决办法
6131
查看次数