gpt4 book ai didi

c++ - 模板元编程 GCC 错误

转载 作者:行者123 更新时间:2023-11-28 03:28:52 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

GCC 4.5.3 中似乎存在错误:

#include <type_traits>

template <bool isFundamentalType, bool isSomething>
struct Foo
{
template <typename T>
static inline void* Do(size_t size);
};

template <>
struct Foo<true, false>
{
template <typename T>
static inline void* Do(size_t size)
{
return NULL;
}
};

template <>
struct Foo<false, false>
{
template <typename T>
static inline void* Do(size_t size)
{
return NULL;
}
};

class Bar
{
};

template <typename T>
int Do(size_t size)
{
// The following fails
return (int) Foo<std::is_fundamental<T>::value, false>::Do<T>(size);
// This works -- why?
return (int) Foo<false, false>::Do<T>(size);
}

int main()
{
return Do<Bar>(10);
}

编译为 g++ 错误.cpp -std=c++0x

错误:

bug.cpp: In function ‘int Do(size_t)’:
bug.cpp:37:65: error: expected primary-expression before ‘>’ token

是否有已知的解决方法可以让我回避这个问题?

编辑:MSVC 2010 成功地编译了它。

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