请考虑以下测试用例(从LLVM源减少):
//% cat foo1.cpp
#include <memory>
namespace {
class A {
int i;
};
}
class G {
std::unique_ptr<A> foo() const;
};
std::unique_ptr<A> G::foo() const { return std::make_unique<A>(); }
Run Code Online (Sandbox Code Playgroud)
和
//% cat foo2.cpp
#include <memory>
namespace {
class A {
bool a;
};
}
class H {
std::unique_ptr<A> bar() const;
};
std::unique_ptr<A> H::bar() const { return std::make_unique<A>(); }
Run Code Online (Sandbox Code Playgroud)
这是否违反了一个定义规则?
gcc-6目前认为如此:
~ % g++ -flto -shared -std=c++14 foo1.cpp foo2.cpp
/home/trippels/gcc_test/usr/local/include/c++/6.0.0/tuple:187:72: warning: type ‘struct _Base’ violates one definition rule [-Wodr]
typedef _Head_base<_Idx, …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
struct D
{
template <class T> D (T);
};
int operator~(const D &);
template <typename T> T &make ();
template <typename Rhs> struct H
{
static const int value = sizeof ~make<Rhs>();
};
enum class E;
int main () { return H<E>::value; }
Run Code Online (Sandbox Code Playgroud)
这是有效的C++ 11吗?
Clang接受了它.Gcc给出了一个错误:
% gcc -std=c++11 b.ii
b.ii: In instantiation of ‘const int H<E>::value’:
b.ii:16:28: required from here
b.ii:11:35: error: no match for ‘operator~’ (operand type is ‘E’)
static const int value = sizeof ~make<Rhs>();
Run Code Online (Sandbox Code Playgroud)
代码从gcc错误报告中删除: …
为什么std :: tuple会分解成右值引用?
#include <tuple>
template <typename, typename> struct same_type;
template <typename T> struct same_type<T, T> {};
void foo() {
std::tuple tuple(1, 'a', 2.3, true);
auto[i, c, d, b] = tuple;
same_type<decltype(i), int &&>{};
same_type<decltype(c), char &&>{};
same_type<decltype(d), double &&>{};
same_type<decltype(b), bool &&>{};
}
Run Code Online (Sandbox Code Playgroud)
使用gcc trunk编译时没有错误.我本来期望普通类型,例如
same_type<decltype(i), int>{};
Run Code Online (Sandbox Code Playgroud)