相关疑难解决方法(0)

范围解析运算符

我偶然碰巧在我看到的一个源代码中找到了这个.所以,我在这里给出了一个类似的小例子.

在文件test.h中:

#include<iostream>

class test{
    int i;
public:
    test(){}
    //More functions here
};
Run Code Online (Sandbox Code Playgroud)

test.cpp文件中:

#include "test.h"

int main()
{
    test test1;
    test::test test2;
    test::test::test test3;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

首先,是否有理由宣布test2这种方式?其次,这个代码在g ++ 4.4.3版和更低版本中编译得很好.C++标准中有什么东西说,当不需要解析范围时,范围解析运算符会被忽略吗?

c++ scope g++

32
推荐指数
2
解决办法
2037
查看次数

为什么允许使用冗余类名限定符?

我遇到了一些像这样的代码:

struct A {
    A() {}
    A(int) {}
};

struct B : A {
    void init(int i);
};

void B::init(int i) {
    A::A(i); // what is this?
}

int main() {
    B b;
    b.init(2);
}
Run Code Online (Sandbox Code Playgroud)

使用VC11 beta进行编译和运行,没有错误或警告/ W4.

明显的意图是调用B :: init重新初始化B的A基础子对象.我相信它实际上解析为一个名为itype 的新变量的变量声明A.使用clang进行编译会产生诊断:

ConsoleApplication1.cpp:11:14: warning: declaration shadows a local variable
        A::A(i);
             ^
ConsoleApplication1.cpp:10:22: note: previous declaration is here
    void B::init(int i) {
                     ^
ConsoleApplication1.cpp:11:14: error: redefinition of 'i' with a different type
        A::A(i);
             ^
ConsoleApplication1.cpp:10:22: note: previous …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer

10
推荐指数
1
解决办法
1719
查看次数

是否可以指定重复的C++类范围?

可能重复:
为什么编译器支持冗余范围限定,是否合法?

我不希望这会编译,但确实如此.这可能是一个编译器错误,还是有一些正确的含义?

$ g++ -c scopes.cpp
$ cat scopes.cpp
class Log {
public:
    Log() { }
    static void fn() { }
};

void test() {
    Log::Log::Log::Log::Log::Log::fn();
}

$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
Run Code Online (Sandbox Code Playgroud)

c++ g++

9
推荐指数
1
解决办法
189
查看次数

为什么decltype(class :: class :: class :: member)有效

我意外地注意到这段代码编译并正常工作:

struct M { int some_int; };
static_assert(std::is_same<
                   decltype(M::M::M::M::some_int) /* <- this */,
                   int>::value, "Types must be int");
Run Code Online (Sandbox Code Playgroud)

为什么这是正确的(decltype(M::M::M::M::some_int) <=> decltype(M::some_int))?

还有哪些其他构造可以使用这种模式class::class::...::member

编译器:用于x86的Microsoft(R)C/C++优化编译器版本19.00.23824.1

c++ decltype c++11

8
推荐指数
2
解决办法
302
查看次数

标签 统计

c++ ×4

g++ ×2

c++11 ×1

decltype ×1

language-lawyer ×1

scope ×1