【问题标题】:ambiguity partial template specialization for member and member functions成员和成员函数的歧义部分模板特化
【发布时间】:2012-04-11 05:16:05
【问题描述】:

我不明白为什么在使用&T::b&T::c 时,由于“类模板实例化不明确”,这段代码无法在main() 中编译。是g++ 4.6.1的bug吗?

#include <iostream>
#include <string>
using namespace std;

struct T{
    int a;
    void b(){}
    int c()
    { 
        return 1; 
    }
};

template<typename CT, CT> struct member_helper;

template<typename FT, FT(T::*mem)> 
struct member_helper<FT(T::*), mem> {
    static string worker()
    { 
        return "for members"; 
    }
};

template<typename Return, typename... Args, Return(T::*fun)(Args...)> 
struct member_helper<Return(T::*)(Args...), fun> {
    static string worker()
    { 
        return "for member functions returning non void"; 
    }
};

template<typename... Args, void(T::*fun)(Args...)> 
struct member_helper<void(T::*)(Args...), fun> {
    static string worker()
    { 
        return "for member functions returning void"; 
    }
};

int main() {
    cout << member_helper<decltype(&T::a), &T::a>::worker();    //prints for members, ok
    cout << member_helper<decltype(&T::b), &T::b>::worker();    //cannot distinguish between all of the three
    cout << member_helper<decltype(&T::c), &T::c>::worker();    //cannot distinguish between member function returning non void and member
}

编辑:

这是完整的错误信息:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp" 
../main.cpp: In function ‘int main()’: 
../main.cpp:27:45: error: ambiguous class template instantiation for ‘struct member_helper’ 
../main.cpp:13:43: error: candidates are: struct member_helper 
../main.cpp:17:78: error: struct member_helper 
../main.cpp:21:59: error: struct member_helper 
../main.cpp:27:8: error: incomplete type ‘member_helper’ used in nested name specifier
../main.cpp:28:45: error: ambiguous class template instantiation for ‘struct member_helper’ 
../main.cpp:13:43: error: candidates are: struct member_helper 
../main.cpp:17:78: error: struct member_helper 
../main.cpp:28:8: error: incomplete type ‘member_helper’ used in nested name specifier make: * [main.o] Errore 1

这是编译器版本:

使用内置规范。 COLLECT_GCC=/usr/bin/g++-4.6.real COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper 目标:x86_64-linux-gnu 配置:../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++, fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr /lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with -sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch- 32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu 线程模型:posix gcc版本4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

【问题讨论】:

  • (@Nawaz: T 是一个真正的类型,上面的结构——虽然真的很混乱)
  • @Mat:有趣。他正在使用模板,并将T 定义为类,造成混乱,尽管英文字母有 26 个字母。
  • @Griwes:使用 4.5.3 编译,但使用 4.6.2 和 4.7.something 失败
  • 除了 full 错误消息(包括为什么编译器认为它们不明确)之外,您还应该包括编译器和版本,因为这是足够棘手的问题,编译器可能无法正确处理它(我觉得不同的部分专业化有一个完整的顺序“返回 void”比“返回非 void”更专业,比“成员”更专业,所以它可能是编译器问题)
  • 我正在尝试在类体内的 typedef 中使用 enable_if 来解决这个问题,而 g++ 4.6.1 崩溃了!!但它似乎在 4.5.1 上运行良好:ideone.com/CbclS

标签: c++ templates c++11 sfinae


【解决方案1】:

这是一个 g++ bug -- 它已在 4.8.x 中修复,供将来遇到此问题的人使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多