使用声明似乎不适用于枚举类型
class Sample{
public:
enum Colour { RED,BLUE,GREEN};
}
using Sample::Colour;
Run Code Online (Sandbox Code Playgroud)
不起作用!! 我们是否需要为枚举类型的每个枚举器添加使用声明?如下
using sample::Colour::RED;
Run Code Online (Sandbox Code Playgroud) 我可以使用SFINAE(或其他技术)进行using声明,而私有派生自模板类吗?为了更好地理解,请参阅以下代
#include <iostream>
struct S1 {
void f() { std::cout << "S1::f\n"; }
};
struct S2 {
void f() { std::cout << "S2::f\n"; }
void g() { std::cout << "S2::g\n"; }
};
template <class T>
struct D : private T {
using T::f;
// using T::g; // need this only if T provides g() function
};
int main() {
D<S1>().f(); // ok. Prints 'S1::f'
D<S2>().f(); // ok. Prints 'S2::f'
D<S2>().g(); // fail. But wants to be ok and …Run Code Online (Sandbox Code Playgroud) 从C++ 11标准,§7.3.3[namespace.udecl]/1:
using声明在声明区域中引入了一个名称,其中出现using声明.
使用声明:
using typenameopt nested-name-specifier unqualified-id;
using ::unqualified-id;using声明中指定的成员名称在using声明出现的声明区域中声明.
在使用声明发生的声明性区域中声明的名称是什么意思?
这是否意味着将该名称引入发生using声明的声明性区域?
声明名称和声明名称所代表的实体之间是否有区别?
例:
namespace N { static int i = 1; } /* Declares an entity denoted by
the name i in the declarative region of the namespace N.
Introduces the name into the declarative region of the namespace N.
Declares the name i in the declarative region of the namespace N? */
using N::i; /* Declares the name i in the declarative …Run Code Online (Sandbox Code Playgroud) using基础构造函数的声明是私有的,但仍然可以构造该类.为什么?
可访问性对于必须公开operator[]的using声明的工作方式不同.
#include <vector>
template<typename T>
class Vec : std::vector<T>
{
private:
using std::vector<T>::vector; // Works, even if private. Why?
public:
using std::vector<T>::operator[]; // must be public
};
int main(){
Vec<int> vec = {2, 2};
auto test = vec[1];
}
Run Code Online (Sandbox Code Playgroud)
如果我希望构造函数是私有的,该怎么办?可以用using声明来完成吗?
struct B1{
int d;
void fb(){};
};
struct B2 : B1{
using B1::d;
using B1::fb;
int d; // why this gives error?
void fb(){} // and this does not?
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
是因为,B1::fb()被视为B1::fb(B1*) and B2::fb()对待B2::fb(B2*)?也就是说,隐含参数,有助于区分这些吗?
$ 13.3.1/4
对于由using声明引入到派生类中的非转换函数,该函数被认为是派生类的成员,用于定义隐式对象参数的类型.
SO最近的一个帖子触发了这一点。
未命名的命名空间被认为等同于
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
Run Code Online (Sandbox Code Playgroud)
我记不起它不等于的确切原因
namespace unique { namespace-body }
using namespace unique;
Run Code Online (Sandbox Code Playgroud)
也尝试过搜索(包括谷歌)但没有成功。请分享您在这方面掌握的任何信息。
c++ namespaces using-declaration language-lawyer unnamed-namespace
新的C++(C++ 0x或C++ 11)有一种新的枚举,一种"枚举类",其中名称的范围是枚举(以及其他内容).
enum class E {
VAL1, VAL2
};
void fun() {
E e = E::VAL1; // Qualified name
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道,如果我可以在某个范围内有选择地使用不合格的名称.就像是:
void fun() {
using E::*;
E e = VAL1;
switch (e) {
case VAL2: ...
Run Code Online (Sandbox Code Playgroud)
我看到我可以写using E::VAL1并获得一个值.但是我不希望为更大的枚举的每个值做到这一点.
在下面,struct Y重载了X成员函数f.两个重载都是模板函数,但是要明确指定不同的参数(typename和int):
struct X
{
template <typename> static bool f() { return true; }
};
struct Y : public X
{
using X::f;
template <int> static bool f() { return false; }
};
int main()
{
std::cout << Y::f <void>() << " " << Y::f <0>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
1 0按预期使用gcc 打印.然而,clang(3.3)抱怨说
[...] error: no matching function for call to 'f'
std::cout << Y::f <void>() << " " << Y::f …Run Code Online (Sandbox Code Playgroud) c++ overloading member-functions using-declaration template-function
让我们考虑下面的代码,其中base成员在derived类中声明。GCC 和 Clang 不同意隐藏哪个成员:
template <class T>
concept C = true;
struct base
{
template <class T>
void foo0 (T&&);
template <class T>
void foo1(T&&) requires C<T>;
template <C T>
void foo2(T&&);
void foo3(C auto &&);
template <C T, class U>
void foo4(T&&, U&&);
template <C T, class U>
void foo5(T&&, U&&);
};
struct derived
: base
{
using base::foo0;
using base::foo1;
using base::foo2;
using base::foo3;
using base::foo4;
using base::foo5;
template <class T>
void foo0(T&&);
template …Run Code Online (Sandbox Code Playgroud) 这段代码编译的原因是什么:
#include <iostream>
using namespace std;
class being {
public:
void running(char c) {
cout << "No one know ";
}
};
class human :public being {
public:
using being::running;
void running(char y) {
cout << "I am a human";
}
};
int main() {
human o;
o.running('A');
return 0;
}
the output : "I am a human"
Run Code Online (Sandbox Code Playgroud)
我的意思是(我期待有错误(人类类中的重定义函数))像这样:这段代码编译:
#include <iostream>
using namespace std;
class being {
public:
int v;
};
struct human :public being {
public:
double v;
}; …Run Code Online (Sandbox Code Playgroud) c++ derived-class using-declaration redefinition language-lawyer
c++ ×9
enums ×2
c++11 ×1
c++20 ×1
constraints ×1
constructor ×1
declaration ×1
entity ×1
inheritance ×1
namespaces ×1
overloading ×1
redefinition ×1
sfinae ×1