在以下代码中,看来g++ 4.7.2被基于using
的类型别名所混淆。
编码:
#include <map>
enum class Direction
{
UP=-1,
DOWN=1
};
template <Direction dir>
struct Comparator
{
bool operator()(int lhs, int rhs) const
{
return lhs<rhs; // Comparison should be based on dir
// but let's not even use dir for now
}
};
template <Direction dir>
using IntToIntMap=std::map<int, int, Comparator<dir>>;
template <Direction dir>
void TestFunc()
{
using TheMap=IntToIntMap<dir>; // TheMap should be a synonym for
// IntToIntMap<dir>
typename IntToIntMap<dir>::value_type value1; // This compiles
typename TheMap::value_type value2; // This does not (???)
}
int main()
{
TestFunc<Direction::UP>();
}
使用以下代码编译代码:
g++ -std=c++11 -Wall --pedantic -o test test.cpp
意外的编译时间错误:
test.cpp: In instantiation of 'void TestFunc() [with Direction dir =
(Direction)-1]
test.cpp:34:29: required from here
test.cpp:29:33: error: no type named 'value_type' in 'using TheMap =
IntToIntMap<dir>'
所讨论的行有什么问题?代码是否违反了C++ 11标准,或者这是g++ 4.7.2错误? Live code in g++-4.7
最佳答案
根据评论,它已在gcc-4.8中修复:
4.7:http://coliru.stacked-crooked.com/view?id=9d55db125baaac0b169e61bbcfcb2a26-f2a3752efd0426bdaa29babef505f76e
4.8:http://coliru.stacked-crooked.com/a/6cd0ad895d067e51