§3.3.6/ 1(C++ 11)
命名空间定义的声明性区域是其namespace-body.原始名称空间名称表示的潜在范围是由同一声明性区域中的每个名称空间定义与原始名称空间名称建立的声明性区域的串联 ....
声明区域的定义如下(§3.3.1/ 1):
每个名称都在程序文本的某些部分中引入,称为声明性区域,该声明区域是该名称有效的程序的最大部分,也就是说,该名称可以用作非限定名称以引用同一实体....
两者结合在一起似乎暗示命名空间的名称只能在命名空间体本身内使用(不合格).但是,显然,这是错误的.那么,当命名空间的名称实际上可以在命名空间体外使用(不合格)时,命名空间定义的声明区域是什么意味着什么呢?
另外,我根本不理解这一点(从上面重新引用):
原始名称空间名称表示的潜在范围是由同一声明性区域中的每个名称空间定义与原始名称空间名称建立的声明性区域的串联 .
该程序将回显"C".我怎么不允许这样做?
import std.stdio;
void main() {
class A {
private void B() {
writeln("C");
}
}
auto D = new A;
D.B();
}
Run Code Online (Sandbox Code Playgroud) 的有效值std::strong_ordering是less,equal,equivalent,和greater。然而,似乎strong_ordering::equivalent和strong_ordering::equal是相等的(即,可互换),因为在这两种情况下,仅展示value字段都为零,并且仅展示构造函数没有其他状态可以初始化。
strong_ordering没有任何单独的 equivalent值是有道理的,因为当排序很强时,等效值总是相等的。但是为 制作strong_ordering::equivalent同义词strong_ordering::equal而不是根本不定义它有什么意义呢?
我有一个参数化查询,如下所示:
SELECT title, html, shortname FROM dbo.Videos
WHERE Topic_ID = ? AND dbo.gradeLevelCheck(?, Grade_Lower, Grade_Upper) = 1
ORDER BY shortname ASC
Run Code Online (Sandbox Code Playgroud)
当我从ASP运行它时,我收到一条错误,内容如下:
Incorrect syntax near the keyword 'from'
Run Code Online (Sandbox Code Playgroud)
参数是56和1(所以没有空值).存储的函数dbo.gradeLevelCheck如下所示:
ALTER FUNCTION [dbo].[gradeLevelCheck]
(
@value int,
@lower int,
@upper int
)
RETURNS int
AS
BEGIN
DECLARE @result int;
IF (@lower IS NULL OR @lower <= @value) AND (@upper IS NULL OR @upper >= @value)
SET @result = 1;
ELSE
SET @result = 0;
RETURN @result; …Run Code Online (Sandbox Code Playgroud) 大多数不可复制的对象都是不可复制的,因为它具有多个(例如,std::unique_ptr)是无意义的或有问题的,但移动它们仍然很好.但有哪些例子有充分的理由使对象既不可复制也不可移动?
我有以下计划:
#include<iostream>
using namespace std;
struct Base01{
int m;
Base01():m(2){}
void p(){cout<<m<<endl;}
};
struct Derived01:public Base01{
Derived01():m(3){}
};
struct Derived02:virtual public Base01{
Derived01():m(4){}
};
struct my: Derived01,Derived02{
my():m(5){}
};
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc/clang都报告编译错误.
我只想知道这里的语言设计考虑是什么,为什么派生类只能在初始化列表中调用基类ctor,但不能直接使用基类成员?
此问题的后续问题
我们有以下代码:
#include <iostream>
struct A
{
static int n;
};
int A::n = 5;
int main()
{
A* a; //uninitialized on purpose
std::cout << a->n; //UB?
}
Run Code Online (Sandbox Code Playgroud)
这样的访问是否为未定义行为?一方面,不需要对象来访问静态类成员,另一方面,operator->在未初始化的指针正在请求麻烦的情况下。
注意:GCC和MSVC会在没有任何警告的情况下编译此代码,Clang抱怨未初始化的用法。https://godbolt.org/z/Gy5fR2
以下内容无法编译:
struct S {
template <class T> S(T) {}
};
void f(int) {}
int main() {
S(f);
}
Run Code Online (Sandbox Code Playgroud)
g ++ - 4.9说
template.cpp: In function ‘int main()’:
template.cpp:6:8: error: no matching function for call to ‘S::S()’
S(f);
^
template.cpp:6:8: note: candidates are:
template.cpp:2:24: note: template<class T> S::S(T)
template <class T> S(T) {}
^
template.cpp:2:24: note: template argument deduction/substitution failed:
template.cpp:6:8: note: candidate expects 1 argument, 0 provided
S(f);
^
template.cpp:1:8: note: constexpr S::S(const S&)
struct S {
^
template.cpp:1:8: note: …Run Code Online (Sandbox Code Playgroud) 我有以下两个班级:
class B;
class A
{
public:
A();
operator B() const;
};
class B
{
public:
B2();
};
Run Code Online (Sandbox Code Playgroud)
这里,A将隐式转换运算符定义为B类.然后C++引用说如下:"如果存在从new_type到表达式类型的隐式转换序列,则不包括左值到右值,数组到指针,函数到指针,空指针,空成员指针,或者布尔转换,然后static_cast可以执行隐式转换的反转".这意味着要编译以下内容
A a;
B b=a;
A a1=static_cast<A> (b);
Run Code Online (Sandbox Code Playgroud)
但Xcode给出了错误信息
如何将私有父类用作内部类的父类?
我试着这样做:
class A
{
};
class B : private A
{
};
class C : private B
{
public:
class D : public A
{
};
};
int main()
{
C c;
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误.有没有办法解决它,或者我需要将私有更改为受保护?
test.cpp:14:20: error: 'A' is a private member of 'A'
class D : public A
^
test.cpp:6:11: note: constrained by private inheritance here
class B : private A
^~~~~~~~~
test.cpp: 1: 7: note: member is declared here
class A
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)