为什么这些复杂的代码可以用GCC 4.7编译,而不能用GCC 4.8

C++ GCC Why this sfinae code can be compiled with GCC 4.7, but not with 4.8?

本文关键字:GCC 编译 不能 代码 为什么 复杂      更新时间:2023-10-16

我喜欢在模板类中使用局部类来执行像"静态if"这样的结构。但是我遇到了gcc 4.8不想编译我的代码的问题。

这个示例:

#include <type_traits>
#include <iostream>
#include <string>
using namespace std;
struct A {
    void printA() {
        cout << "I am A" << endl;
    }
};
struct B {
    void printB() {
        cout << "I am B" << endl;
    }
};
template <typename T>
struct Test {
    void print() {
        struct IfA {
            constexpr IfA(T &value) : value(value) {
            }
            T &value;
            void print() {
                value.printA();
            }
        };
        struct IfB {
            constexpr IfB(T &value) : value(value) {
            }
            T &value;
            void print() {
                value.printB();
            }
        };
        struct Else {
            constexpr Else(...) {}
            void print() {
            }
        };
        typename conditional<is_same<T, A>::value, IfA, Else>::type(value).print();
        typename conditional<is_same<T, B>::value, IfB, Else>::type(value).print();
    }
    T value;
};
int main() {
    Test<A>().print();
    Test<B>().print();
}

选项:

g++ --std=c++11 main.cc -o local-sfinae
任务:

  1. 给定A类和B类具有不同的打印接口。
  2. 写一个泛型类Test,可以打印a和b。不要污染任何命名空间或类的作用域。

代码描述:

  1. 我使用这样的方法,因为我想概括"静态if"的构造。看,我通过IfA和IfB类的字段传递参数,而不是直接传递给print()函数。
  2. 我经常使用这样的结构。
  3. 我发现这些建筑不应该在(污染)类范围内。我的意思是它们应该放在一个方法作用域中。

所以这个问题。

此代码不能用GCC 4.8编译。因为它检查所有类,即使它们从未使用过。但是它没有以二进制实例化它们(我已经注释了导致错误的行,并用gcc 4.8进行了编译)。证明:

$ nm local-sfinae |c++filt |grep "::If.*print"
0000000000400724 W Test<A>::print()::IfA::print()
00000000004007fe W Test<B>::print()::IfB::print()

看,没有Test::print()::IfB::print()。(见后:"空白试验::打印()::招标::打印()(T =]")

使用gcc 4.8编译上述代码的错误:

g++ --std=c++11 main.cc -o local-sfinae
main.cc: In instantiation of 'void Test<T>::print()::IfB::print() [with T = A]':
main.cc:36:9:   required from 'void Test<T>::print() [with T = A]'
main.cc:49:21:   required from here
main.cc:34:17: error: 'struct A' has no member named 'printB'
                 value.printB();
                 ^
main.cc: In instantiation of 'void Test<T>::print()::IfA::print() [with T = B]':
main.cc:28:9:   required from 'void Test<T>::print() [with T = B]'
main.cc:50:21:   required from here
main.cc:26:17: error: 'struct B' has no member named 'printA'
                 value.printA();
                 ^
  1. 是GCC 4.8的bug吗?
  2. 还是GCC 4.7的bug?也许代码不应该编译。
  3. 或者这是我的错误,我不应该依赖编译器的行为/不应该使用这种方法来实现"静态if"。

额外的信息:

这个简单的代码可以在4.7上编译,但不能在4.8上编译。我把它缩短了。

struct A {
    void exist() {
    }
};
template <typename T>
struct Test {
    void print() {
        struct LocalClass {
            constexpr LocalClass(T &value) : value(value) {
            }
            T &value;
            void print() {
                value.notExist();
            }
        };
    }
    T value;
};
int main() {
    Test<A>().print();
}

错误:

main.cc: In instantiation of 'void Test<T>::print()::LocalClass::print() [with T = A]':
main.cc:16:9:   required from 'void Test<T>::print() [with T = A]'
main.cc:22:21:   required from here
main.cc:14:17: error: 'struct A' has no member named 'notExist'
                 value.notExist();
                 ^

测试了两个GCC 4.8版本:2012.10和2013.02。

LocalClass不是模板。"未使用则未实例化"规则只适用于类模板的成员函数。

也就是说,当Test::print()被实例化时,里面的所有东西都被激活,包括它的局部类中未使用的成员。

您的代码中没有SFINAE。

SFINAE应用于模板参数推导和参数替换(SFINAE中的'S'代表替换),但程序中的唯一替换发生在将Test的模板参数列表中的A替换为T时,该替换不会失败。

然后调用实例化Test<A>::print()print(),它不涉及任何替换,你会得到一个错误,因为value.notExist();无效。

SFINAE必须在替换上下文中使用,例如由函数调用引起的模板实参推导或使用默认实参推导模板形参时。