2012-03-27 81 views
1

我不明白爲什麼這個代碼無法使用&T::b&T::cmain()編譯由於「曖昧類模板實例」。它是g ++ 4.6.1的缺陷嗎?歧義模板偏特供成員和成員函數

#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 

,這是copmiler版本:

使用內置的規格。 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-9u buntu3)

+1

發佈完整的錯誤消息。 – Nawaz 2012-03-27 16:19:08

+1

(@Nawaz:T是一個真正的類型,上面的結構 - 雖然很混亂) – Mat 2012-03-27 16:22:59

+0

@Mat:有趣。他正在使用模板,並已將「T」定義爲類,從而造成混淆,即使英文字母有26個字母。 – Nawaz 2012-03-27 16:24:16

回答

1

這是一個g ++ bug - 對於那些在未來碰到這個問題的人來說,它被固定在4.8.x中。