在下面的代码中,我试图让一个朋友函数访问该类的私有成员.根据我的理解,我正确地将其声明为朋友功能,但VS2012给了我错误:
error C2248: 'X::S::s_' : cannot access private member declared in class 'X::S'
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我做错了什么?这是最简单的示例,演示了我可以提出的编译器错误.
namespace X
{
class S
{
friend std::string r(X::S &s);
std::unique_ptr<std::istream> s_;
};
}
std::string r(X::S &s)
{
auto& x = s.s_;
return "";
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Visual Studio 2013 RTM.
我正在尝试编写一个匹配多个字符之一的可变参数模板.递归案例很容易,但我正在努力编写基本案例.
template <char C, char... Cs>
auto char_match(char c) -> bool
{
return c == C || char_match<Cs...>(c);
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下作为基本案例,但它没有用.我知道你可以用类模板做到这一点,我很确定你不能用功能模板做到这一点.
template <>
auto char_match(char c) -> bool
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
错误C2912:'bool char_match(char)'的显式特化不是函数模板的特化
我也尝试在返回类型上使用std :: enable_if,但微软并不喜欢它.
template <char C, char... Cs>
typename std::enable_if<sizeof...(Cs) != 0, bool>::type char_match(char c)
{
return c == C || char_match<Cs...>(c);
}
template <char C, char... Cs>
typename std::enable_if<sizeof...(Cs) == 0, bool>::type char_match(char c)
{
return c == C;
}
Run Code Online (Sandbox Code Playgroud)
错误C2039:'type':不是'std :: …
在下面的代码中,对象'queue'是不可复制的,但由于std :: mutex而可以移动.
std::generate_n(std::back_inserter(thread_pool),
std::thread::hardware_concurrency,
[&](){return std::thread(handler(), exiting, queue);});
Run Code Online (Sandbox Code Playgroud)
由于互斥锁上的私有拷贝构造函数,VC++ 2012无法编译.它无法为队列生成复制构造函数.为什么要尝试复制队列?在我看来,一切都是通过参考,因此没有副本.
我正在使用VS2015 Update 3.我有一个函数,我希望根据可调用对象的返回类型进行专门化.当可调用对象是一个仿函数时,一切都按预期工作.当可调用对象是函数或函数指针时,它无法专门化重载函数.我觉得我错过了一些明显的东西,但是我在一年多的时间里没有对SFINAE做过任何事情.
我错过了什么导致专业化失败?
template <typename T>
struct S
{
T mOp;
template <typename = void>
typename std::enable_if<
std::is_same<
std::remove_cv_t<
std::remove_reference_t<
decltype(mOp())
>
>,
void
>::value
>::type func()
{
std::cout << "bool" << std::endl;
}
template <typename = void>
typename std::enable_if<
std::is_same<
std::remove_cv_t<
std::remove_reference_t<
decltype(mOp())
>
>,
bool
>::value
>::type func()
{
std::cout << "void" << std::endl;
}
};
template <typename T>
auto createS(T&& t)
{
return S<T>{ t };
}
void vfunc()
{
}
bool bfunc()
{
return true; …Run Code Online (Sandbox Code Playgroud) 我正在使用wlgGetProcAddress使用创建的上下文来获取函数wglCreateContext.我已经设置了上下文wglMakeCurrent.我得到一个有效的函数指针glGetStringi,但我得到NULL了glGetString.我认为glGetString并且glGetStringi是在相同版本的OpenGL(1.0)中引入的.任何想法为什么我得到这个NULL?
const GLubyte* (*glGetString)(GLenum);
const GLubyte* (*glGetStringi)(GLenum, GLuint);
glGetString = reinterpret_cast<decltype(glGetString)>(wglGetProcAddress("glGetString"));
glGetStringi = reinterpret_cast<decltype(glGetStringi)>(wglGetProcAddress("glGetStringi"));
Run Code Online (Sandbox Code Playgroud)
如果它很重要,我有一个驱动程序版本13.251.0.0的Radeon HD 7950.
在尝试实现时,std::is_function我遇到了VC++和libc ++不同意的情况.
static_assert(std::is_function<int()volatile &>::value, "Not function");
Run Code Online (Sandbox Code Playgroud)
libc ++接受这个.VC++失败,声称它不是一个函数.谁是对的?
在20.9.4.1(表47)中,它说:
template <class T> struct is_function;
T is a function type (3.9.2)
Run Code Online (Sandbox Code Playgroud)
短语"功能类型"没有明确指定为"整数类型".我很难找到它的确切含义.哪些段落准确指出"功能类型"的含义?
我有一些代码,我正在编译嵌入式平台.当我尝试针对代码运行基于Clang的工具时,我遇到了一些错误.
我已将生成错误的代码缩小到以下内容:
typedef int _GCC_ATTR_ALIGN_8t __attribute__((__mode__(__QI__)));
typedef _GCC_ATTR_ALIGN_8t _Int8t __attribute__((__aligned__(1)));
typedef _Int8t _Intleast8t;
typedef _Int8t _Intfast8t;
typedef _Int8t _int8;
typedef _Int8t int8_t;
Run Code Online (Sandbox Code Playgroud)
运行clang 3.8.0对代码的输出是:
x.cpp:5:16: error: cannot combine with previous 'type-name' declaration
specifier
typedef _Int8t _int8;
^
x.cpp:5:1: warning: typedef requires a name [-Wmissing-declarations]
typedef _Int8t _int8;
^~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
Run Code Online (Sandbox Code Playgroud)
为什么它仅在_int8 typedef而不是其他typedef上有问题?此外,错误是什么意思?