我用GCC编译这段代码(4.2.1 Apple build 5664)
#include <cstddef>
using std::size_t;
template <char I> struct index { };
struct a
{
void operator()(size_t const &) { }
};
struct b
{
template <char I>
void operator()(index<I> const &) { }
};
struct c: public a, public b { };
int main (int argc, char const *argv[])
{
c vc;
vc(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并给我以下错误:
main.cpp: In function ‘int main(int, const char**)’:
main.cpp:22: error: request for member ‘operator()’ is ambiguous
main.cpp:14: error: candidates are: …Run Code Online (Sandbox Code Playgroud) 以下代码使用GCC(4.2-4.6)和Clang(2.1)编译得很好,但是当我运行可执行文件时,它给出了"总线错误:10".我不明白原因.
#include <iostream>
struct A
{
static int const v;
A() { ++*const_cast<int *>(&A::v); }
};
int const A::v = 0;
int main(int argc, char * argv[])
{
A a, b, c;
std::cout << a.v << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用可变参数模板来指定朋友类。我尝试使用以下语法,但是不起作用。
template <class... Args>
struct A {
friend Args...;
};
Run Code Online (Sandbox Code Playgroud)
我尝试编写一些变通办法,但是由于友谊不是可传递的和继承的,因此似乎并不是那么简单。因此,问题是是否存在正确的语法或任何变通方法,以使Args中的每个单独的类成为A的朋友?
c++ ×3
c++11 ×1
const ×1
const-cast ×1
friend ×1
inheritance ×1
member ×1
static ×1
templates ×1