如果您查看语法,*declarator*s in §8/4您会注意到a noptr-declarator可以写成(ptr-declarator),也就是说,它可以写成(declarator-id),它可以验证标题中的声明.事实上,这段代码编译没有问题:
#include <iostream>
struct A{ int i;};
int (x) = 100;
A (a) = {2};
int main()
{
std::cout << x << '\n';
std::cout << a.i << '\n';
}
Run Code Online (Sandbox Code Playgroud)
但是当声明中没有涉及指针(数组或函数)时允许这些括号的目的是什么?
在草案C++ 11标准:N3337中,我发现了几个引用top-level cv-qualifiers,但没有定义.
我正在努力理解这个规则,特别是下面的粗体句子(我的重点):
考虑注释#2在下面的代码片段:这是什么意思是说,功能类型f(int),但是t是const?
§14.8.2/3:执行此替换后,将执行8.3.5中描述的功能参数类型调整.[示例:参数类型"
void ()(const int, int[5])"变为"void(*)(int,int*)".-end example] [注意:函数参数声明中的顶级限定符不会影响函数类型,但仍会影响函数中函数参数变量的类型.- 尾注 ] [示例:Run Code Online (Sandbox Code Playgroud)template <class T> void f(T t); template <class X> void g(const X x); template <class Z> void h(Z, Z*); int main() { // #1: function type is f(int), t is non const f<int>(1); // #2: function type is f(int), t is const f<const int>(1); // #3: function type is g(int), x is const g<int>(1); // #4: …
在§13.3.1/ 4(N3337)中,您将找到以下内容:
对于静态成员函数,隐式对象参数被认为与任何对象匹配(因为如果选择该函数,则丢弃该对象).
§9.4.1/ 2有这样的断言:
静态成员函数没有this指针.
那么,静态成员函数的隐式对象参数的目的是什么?
根据§13.1/ 2,下面的两个成员函数不会超载.为什么是这样?
class X {
static void f();
void f() const;
};
Run Code Online (Sandbox Code Playgroud)
编辑:问题不重复.上面的例子显示了一个const函数和一个非const的静态函数.我从§13.1/ 2中知道函数不可重载.我只是想知道为什么标准不允许上面给出的结构.
这是我先前问题的延续.请注意,声明void (*pf)(int) = bar;会触发static_assert.我不明白为什么.另请注意,如果我在此声明中替换bar,bar<const int>则代码将进行编译.
#include <iostream>
#include <type_traits>
template<typename T>
void bar(T t)
{
static_assert(std::is_same<T, const int>::value, "Error!");
std::cout << t << '\n';
}
int main()
{
// static_assert doesn't fire because T==const int
bar<const int>(1);
// But here static_assert fires because T==int (see the error message). Why is this?
// If I replace `bar` by `bar<const int>` below the code compiles.
void(*pf)(int) = bar;
pf(1000);
}
Run Code Online (Sandbox Code Playgroud)
请考虑以下代码段:
#include <iostream>
struct S {
virtual void pure1() = 0;
virtual void pure2() = 0;
};
struct T : S {
void pure1() { std::cout << "T::pure1" << '\n'; }
void pure2() { std::cout << "T::pure2" << '\n'; }
};
void S::pure2() { std::cout << "S::pure2" << '\n';}
int main()
{
T t;
t.S::pure2();
}
Run Code Online (Sandbox Code Playgroud)
它打印S::pure2.
看看C++ 11标准,我不确切知道这是怎么发生的.我认为这与§3.4.5/ 4有关:
如果类成员访问中的id-expression是表单的qualified-id
类名称或名称空间名称:: ...
下面的class-name-or-namespace-name.或 - >运算符首先在对象表达式的类中查找,如果找到,则使用名称.否则,它将在整个postfix-expression的上下文中查找 .
但我不明白如何使用上面的表达式pure2()在基类中找到纯虚函数.S …
§14.8.2/ 4允许实例化两个不同的函数,g<int>并g<const int>从模板定义中实现.为什么标准不允许在f下面的代码中定义这两个函数?我知道两个函数都有相同的类型void(int).但这也发生在实例化的函数中g.§14.8.2/ 4中的注释说:f<int>(1) and f<const int>(1) call distinct functions even though both of the functions called have the same function type..
#include <iostream>
template<typename T>
void g(T t) { std::cout << t << '\n'; }
void f(int i) { std::cout << i << '\n'; }
//void f(int const i) { std::cout << i << '\n'; } // doesn't compile
int main()
{
g<int>(1);
g<int const>(2);
}
Run Code Online (Sandbox Code Playgroud) std::remove_reference 使用以下实现:
template< class T > struct remove_reference {typedef T type;};
template< class T > struct remove_reference<T&> {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};
Run Code Online (Sandbox Code Playgroud)
因此,如果一个std::remove_reference<int&>类型int&将匹配T&专业化.如果我们使用std::remove_reference<int&&>类型int&&匹配T&&专业化.
我想知道标准中的哪条规则描述了这个匹配过程.
我从MS的此页面获得以下示例,但由于此消息,代码未链接:
错误LNK2019:函数_wmain中引用的未解析的外部符号__imp__DWriteCreateFactory @ 12
我重复以下代码以方便阅读:
#include <dwrite.h>
#include <string.h>
#include <stdio.h>
#include <new>
// SafeRelease inline function.
template <class T> inline void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
void wmain()
{
IDWriteFactory* pDWriteFactory = NULL;
HRESULT hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory)
);
IDWriteFontCollection* pFontCollection = NULL;
// Get the system font collection.
if (SUCCEEDED(hr))
{
hr = pDWriteFactory->GetSystemFontCollection(&pFontCollection);
}
UINT32 familyCount = 0;
// Get the number of font families in …Run Code Online (Sandbox Code Playgroud)