gpt4 book ai didi

c++ - 成员和成员函数的歧义部分模板特化

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:52 25 4
gpt4 key购买 nike

我不明白为什么在使用 &T::b 时,由于“模棱两可的类模板实例化”,此代码无法在 main() 中编译&T::c。是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

这是 copmiler 版本:

Using built-in specs. COLLECT_GCC=/usr/bin/g++-4.6.real COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../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 Thread model: posix gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

最佳答案

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

关于c++ - 成员和成员函数的歧义部分模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com