成员和成员函数的模糊部分模板专门化
ambiguity partial template specialization for member and member functions
我不明白为什么在使用&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
这是copmiler版本:
使用内置规格。COLLECT_GCC=/usr/bin/g++-4.6.realCOLLECT_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"--带有bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs--启用语言=c,c++,fortran,objc,obj-c++,go--prefix=/usr/--program suffix=-4.6--enable shared--enable linker build id--带系统zlib--libexecdir=/usr/lib--不带gettext--enable threads=posix--带gxx include dir=/usr/include/c++/4.6--libdir=/usr/lib--enable nls--带sysroot=/--enable clocale=gnu--enable libstdcxx debug--enable librstdcxx 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-g努线程模型:posix gcc 4.6.1版(Ubuntu/Linaro 4.6.1-9ubuntu3)
这是一个g++错误,它在4.8.x中为将来遇到它的人修复了。
- 如何基于模板化类的基类专门化成员函数
- 根据成员容器的尺寸,专门化成员功能
- 专门化一个成员函数,而不是整个类
- c++多参数模板化的类成员专门化
- C++ : 如何为模板类 A 专门化成员函数来处理类似数组的类 A<T>< >?
- 使用已推导的模板参数专门化模板成员函数
- C++专门化成员功能
- 根据类成员的存在/不存在专门化C++模板
- 如何根据类的模板参数专门化模板成员函数?
- 如何为具有位字段成员的类专门化“swap”
- 成员和成员函数的模糊部分模板专门化
- C++中模板类的成员函数的专门化
- 专门化模板成员函数来处理std::string和char[]参数
- 如何专门化此模板成员函数
- C++-专门化Template类的成员函数
- 专门化模板成员函数
- 可变模板成员函数的部分专门化
- 类型组模板化类的成员专门化
- 部分成员专门化
- 使用IBM Rational Rhapsody的显式成员专门化