[英]sfinae on member function defined outside of class body
Kind of a continuation from my previous question . 这是我上一个问题的延续。 What I've got is a bunch of functions that form a sfinae depencency chain like so (let "A -> B" notation mean that existence of A depends on existence of B):
我得到的是一堆函数,形成像这样的sfinae依赖链(让“A - > B”符号表示A的存在取决于B的存在):
S::f_base -> S::f -> ns::f_ -> f -> T::f
where T is the template argument. 其中T是模板参数。 It's implemented like this :
它实现像这样 :
#include <utility>
struct S;
template <typename T>
auto f(S& s, T const& t) -> decltype(t.f(s), void())
{
t.f(s);
}
namespace ns
{
template <typename T>
auto f_(S& s, T const& t) -> decltype(f(s, t), void())
{
f(s, t);
}
}
struct S
{
template <typename T>
auto f(T const& t) -> decltype(ns::f_(std::declval<S&>(), t), void())
{
ns::f_(*this, t);
}
template <typename T>
auto f_base(T const* t_ptr) -> decltype(f(*t_ptr), void())
{
f(*t_ptr);
}
};
struct pass
{
void f(S&) const
{
}
};
struct fail
{
};
int main()
{
S s;
s.f(pass()); // compiles
//s.f(fail()); // doesn't compile
return 0;
}
and works as intended. 并按预期工作。 The problem arises when I attempt to move definitions of
S::f
and S::f_base
outside the class body, like so : 当我试图移动的定义问题出现
S::f
和S::f_base
类主体以外,像这样 :
#include <utility>
struct S;
template <typename T>
auto f(S& s, T const& t) -> decltype(t.f(s), void())
{
t.f(s);
}
namespace ns
{
template <typename T>
auto f_(S& s, T const& t) -> decltype(f(s, t), void())
{
f(s, t);
}
}
struct S
{
template <typename T>
auto f(T const& t) -> decltype(ns::f_(std::declval<S&>(), t), void());
template <typename T>
auto f_base(T const* t_ptr) -> decltype(f(*t_ptr), void());
};
template <typename T>
auto S::f(T const& t) -> decltype(ns::f_(std::declval<S&>(), t), void())
{
ns::f_(*this, t);
}
template <typename T>
auto S::f_base(T const* t_ptr) -> decltype(f(*t_ptr), void()) // <---- HERE ---
{
f(*t_ptr);
}
int main()
{
return 0;
}
On the line marked by arrow GCC 4.7.1
expresses it's dissatisfaction: 在箭头标记为
GCC 4.7.1
表示不满:
error: prototype for 'decltype ((((S*)0)->S::f((* t_ptr)), void())) S::f_base(const T*)' does not match any in class 'S'
错误:'decltype的原型((((S *)0) - > S :: f((* t_ptr)),void()))S :: f_base(const T *)'与类中的任何一个都不匹配' S”
error: candidate is: template decltype ((((S*)this)->S::f((* t_ptr)), void())) S::f_base(const T*)错误:候选者是:模板decltype(((((S *)this) - > S :: f((* t_ptr)),void()))S :: f_base(const T *)
I tried to explicitly specify which f
I'm using in f_base
by prepending it (in both declaration and definition) with std::declval<S&>().
我尝试通过在
std::declval<S&>().
添加它(在声明和定义中)来明确指定我在f_base
使用的f
std::declval<S&>().
, but the error persists. ,但错误仍然存在。
I know I can modify the dependency graph like so: 我知道我可以像这样修改依赖图:
S::f_base ->
-> ns::f_ -> f -> T::f
S::f ->
to make S::f_base
depend on ns::f_
along with S::f
, but is there a way to do this with the first dependency graph? 使
S::f_base
依赖于ns::f_
以及S::f
,但有没有办法用第一个依赖图做到这一点?
GCC 4.X is not the best for meta programing GCC 4.X不是元编程的最佳选择
I have manage to make it compile in 4.7.3 ( live ) : 我已经设法在4.7.3( 实时 )中编译:
#include <utility>
struct S;
template <typename T>
auto f(S& s, T const& t) -> decltype(t.f(s), void())
{
t.f(s);
}
namespace ns
{
template <typename T>
auto f_(S& s, T const& t) -> decltype(f(s, t), void())
{
f(s, t);
}
}
// some std::void_t like but GCC4.x compatible
template<class T>
struct void_t
{
using type = typename std::enable_if<std::is_same<T,T>::value >::type;
};
struct S
{
template <typename T>
auto f(T const& t) -> decltype(ns::f_(std::declval<S&>(), t), void());
template <typename T>
auto f_base(T const* t_ptr) -> typename void_t< decltype (::f(*std::declval<T const*>()))>::type ;
};
template <typename T>
auto S::f(T const& t) -> decltype(ns::f_(std::declval<S&>(), t), void())
{
ns::f_(*this, t);
}
// ::f is needed, fail if just 'f'
template <typename T>
auto S::f_base(T const* t_ptr) -> typename void_t< decltype (::f(*std::declval<T const*>()))>::type
{
f(*t_ptr);
}
int main()
{
return 0;
}
Note: 注意:
In GCC4.XI have seen some weird stuff like : 在GCC4.XI中看到过一些奇怪的东西,比如:
template<class T>
struct void_t
{
using type = void;
};
Where GCC replace void_t<decltype(expr)>::type
by void
even if expr
is not valid. 其中GCC将
void_t<decltype(expr)>::type
替换为void
即使expr
无效。 That why I use this implementation of void_t
. 这就是为什么我使用
void_t
这个实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.