C++ 在C++;MSVC和x2B上的11个+;14 问题
这个问题涉及到关于函数指针的几个问题(我不会列出它们),但最重要的是它涉及到发布的代码。《华盛顿邮报》声称,该代码应该符合标准,并且在Apple Clang上编译得很好 我还没有设法在VS2015(即MSVC++14)中编译它 此错误的来源是什么?为什么不使用Clang?C++ 在C++;MSVC和x2B上的11个+;14 问题,c++,c++11,visual-c++,delegates,visual-studio-2015,C++,C++11,Visual C++,Delegates,Visual Studio 2015,这个问题涉及到关于函数指针的几个问题(我不会列出它们),但最重要的是它涉及到发布的代码。《华盛顿邮报》声称,该代码应该符合标准,并且在Apple Clang上编译得很好 我还没有设法在VS2015(即MSVC++14)中编译它 此错误的来源是什么?为什么不使用Clang? 第一个错误 第一个错误重复多次: ...\Delegate.hpp(329): error C2514: 'Delegate<R(A...)>::is_member_pair<<unnamed-sym
第一个错误 第一个错误重复多次:
...\Delegate.hpp(329): error C2514: 'Delegate<R(A...)>::is_member_pair<<unnamed-symbol>>':
class has no constructors
第二个错误 出现多个错误,但第一个错误通常最重要:
...\Delegate.hpp(340): error C2059: syntax error: '<end Parse>'
...\Delegate.hpp(349): note: see reference to class template instantiation 'Delegate<R(A...)>' being compiled
…\Delegate.hpp(340):错误C2059:语法错误:“”
…\Delegate.hpp(349):注意:请参阅对正在编译的类模板实例化“Delegate”的引用
然后遵循一些语法错误、无法识别的模板声明/定义等。相关代码:
template <typename T>
static typename ::std::enable_if<
is_member_pair<T>{} ||
is_const_member_pair<T>{},
R
>::type
functor_stub(void* const object_ptr, A&&... args)
{
return (static_cast<T*>(object_ptr)->first->*
static_cast<T*>(object_ptr)->second)(::std::forward<A>(args)...);
}
模板
静态类型名称::std::启用\u如果<
是成员对{}||
是常量成员对{},
R
>::类型
函子_存根(void*const object_ptr,A&&…args)
{
返回(静态\u转换(对象\u ptr)->first->*
静态强制转换(对象ptr)->秒(::标准::向前和向后)
#pragma一次
#ifndef代表(水电站)
#定义代理\u HPP
#包括
#包括
#包括
#包括
#包括
模板类委托;
模板
类委托
{
使用stub_ptr_type=R(*)(void*,A&…);
委托(无效*常数o,存根类型常数m)无例外:
对象ptr(o),
存根ptr(m)
{
}
公众:
Delegate()=默认值;
委托(委托常数&)=默认值;
委托(委托&&)=默认值;
委托(::std::nullptr_t const)noexcept:Delegate(){}
模板::类型>
显式委托(C常量*常量o)无例外:
对象(const_cast(o))
{
}
模板::类型>
显式委托(C常量和o)无例外:
对象(常量转换(&o))
{
}
模板
委托(C*常量对象,R(C::*常量方法)(A…)
{
*这=来自(对象\u ptr,方法\u ptr);
}
模板
委托(C*常量对象,R(C::*常量方法)(A…)常量)
{
*这=来自(对象\u ptr,方法\u ptr);
}
模板
委托人(C&object,R(C::*常量方法(A…)
{
*这=来自(对象、方法\u ptr);
}
模板
代表(C常量和对象,R(C::*常量方法_ptr)(A…)常量)
{
*这=来自(对象、方法\u ptr);
}
模板<
类型名T,
typename=typename::std::启用\u如果<
!::std::是否相同{}
>::类型
>
代表(T&f):
存储区(新运算符(sizeof(typename::std::decay::type)),
函子,
存储大小(sizeof(typename::std::decation::type))
{
使用functor_type=typename::std::decay::type;
新的(store.get())函子类型(::std::forward(f));
object_ptr_u2;=store_2;.get();
存根=函子存根;
删除器=删除器存根;
}
委托和运算符=(委托常量&)=默认值;
委托和运算符=(委托和)=默认值;
模板
代表和操作员=(R(C::*const rhs)(A…)
{
return*this=from(static_cast(object_ptr_)),rhs;
}
模板
代表和操作员=(R(C::*常数rhs)(A…)常数)
{
return*this=from(static_cast(object_ptr_)),rhs;
}
模板<
类型名T,
typename=typename::std::启用\u如果<
!::std::是否相同{}
>::类型
>
委托和运算符=(T和f)
{
使用functor_type=typename::std::decay::type;
if((sizeof(functor_type)>store_size_124;||!store_124;.unique())
{
store_u.reset(新运算符(sizeof(函子类型)),
函子;
存储大小=sizeof(函子类型);
}
其他的
{
deleter(store.get());
}
新的(store.get())函子类型(::std::forward(f));
object_ptr_u2;=store_2;.get();
存根=函子存根;
删除器=删除器存根;
归还*这个;
}
模板
静态委托自()noexcept
{
返回{nullptr,function_stub};
}
模板
来自(C*常量对象_ptr)的静态委托无异常
{
返回{object_ptr,method_stub};
}
模板
来自(C常量*常量对象_ptr)的静态委托无异常
{
返回{const_cast(object_ptr),const_method_stub};
}
模板
来自(C&object)的静态委托不例外
{
返回{&object,方法_stub};
}
模板
来自(C常量和对象)noexcept的静态委托
{
返回{const_cast(&object),const_method_stub};
}
模板
来自(T&f)的静态委托
{
返回::标准::转发(f);
}
来自(R(*const function_ptr)(A…)的静态委托
{
返回函数;
}
模板
使用成员对=
::std::对;
模板
使用常量成员对=
::std::对;
模板
来自(C*常量对象)的静态委托,
R(C::*常量方法(A…)
{
返回成员对(对象、方法);
}
模板
来自(C常量*常量对象)的静态委托,
R(C::*常数方法(A…)常数)
{
返回常量成员对(对象、方法);
}
模板
来自的静态委托(C&object,R(C::*const method_ptr)(A…)
{
返回成员对(&对象,方法\u ptr);
}
模板
来自的静态委托(C常量和对象,
R(C::*常数方法(A…)常数)
{
返回常量成员对(&对象,方法\u ptr);
}
void reset()
void reset_stub()noexcept{stub_ptr_u2;=nullptr;}
无效交换(委托和其他)无例外{::std::swap(*this,other);}
布尔运算符==(委托常量和rhs)常量noexcept
{
返回(object_ptr_==rhs.object_ptr_)&&(stub_ptr_==rhs.stub_ptr_);
}
布尔运算符!=(代理常量和rhs)常量无异常
{
返回!运算符==(rhs);
}
bool算子~T();
运算符删除(p);
}
模板
静态void deleter_存根(void*const p)
{
静态_cast(p)->~T();
}
模板
静态R函数_存根(void*const,A&&…args)
{
返回函数_ptr(::std::forward(args)…);
}
模板
静态R方法_存根(vo
template <typename T>
static typename ::std::enable_if<
is_member_pair<T>{} ||
is_const_member_pair<T>{},
R
>::type
functor_stub(void* const object_ptr, A&&... args)
{
return (static_cast<T*>(object_ptr)->first->*
static_cast<T*>(object_ptr)->second)(::std::forward<A>(args)...);
}
#pragma once
#ifndef DELEGATE_HPP
#define DELEGATE_HPP
#include <cassert>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
template <typename T> class Delegate;
template<class R, class ...A>
class Delegate<R (A...)>
{
using stub_ptr_type = R (*)(void*, A&&...);
Delegate(void* const o, stub_ptr_type const m) noexcept :
object_ptr_(o),
stub_ptr_(m)
{
}
public:
Delegate() = default;
Delegate(Delegate const&) = default;
Delegate(Delegate&&) = default;
Delegate(::std::nullptr_t const) noexcept : Delegate() { }
template <class C, typename =
typename ::std::enable_if< ::std::is_class<C>{}>::type>
explicit Delegate(C const* const o) noexcept :
object_ptr_(const_cast<C*>(o))
{
}
template <class C, typename =
typename ::std::enable_if< ::std::is_class<C>{}>::type>
explicit Delegate(C const& o) noexcept :
object_ptr_(const_cast<C*>(&o))
{
}
template <class C>
Delegate(C* const object_ptr, R (C::* const method_ptr)(A...))
{
*this = from(object_ptr, method_ptr);
}
template <class C>
Delegate(C* const object_ptr, R (C::* const method_ptr)(A...) const)
{
*this = from(object_ptr, method_ptr);
}
template <class C>
Delegate(C& object, R (C::* const method_ptr)(A...))
{
*this = from(object, method_ptr);
}
template <class C>
Delegate(C const& object, R (C::* const method_ptr)(A...) const)
{
*this = from(object, method_ptr);
}
template <
typename T,
typename = typename ::std::enable_if<
!::std::is_same<Delegate, typename ::std::decay<T>::type>{}
>::type
>
Delegate(T&& f) :
store_(operator new(sizeof(typename ::std::decay<T>::type)),
functor_deleter<typename ::std::decay<T>::type>),
store_size_(sizeof(typename ::std::decay<T>::type))
{
using functor_type = typename ::std::decay<T>::type;
new (store_.get()) functor_type(::std::forward<T>(f));
object_ptr_ = store_.get();
stub_ptr_ = functor_stub<functor_type>;
deleter_ = deleter_stub<functor_type>;
}
Delegate& operator=(Delegate const&) = default;
Delegate& operator=(Delegate&&) = default;
template <class C>
Delegate& operator=(R (C::* const rhs)(A...))
{
return *this = from(static_cast<C*>(object_ptr_), rhs);
}
template <class C>
Delegate& operator=(R (C::* const rhs)(A...) const)
{
return *this = from(static_cast<C const*>(object_ptr_), rhs);
}
template <
typename T,
typename = typename ::std::enable_if<
!::std::is_same<Delegate, typename ::std::decay<T>::type>{}
>::type
>
Delegate& operator=(T&& f)
{
using functor_type = typename ::std::decay<T>::type;
if ((sizeof(functor_type) > store_size_) || !store_.unique())
{
store_.reset(operator new(sizeof(functor_type)),
functor_deleter<functor_type>);
store_size_ = sizeof(functor_type);
}
else
{
deleter_(store_.get());
}
new (store_.get()) functor_type(::std::forward<T>(f));
object_ptr_ = store_.get();
stub_ptr_ = functor_stub<functor_type>;
deleter_ = deleter_stub<functor_type>;
return *this;
}
template <R (* const function_ptr)(A...)>
static Delegate from() noexcept
{
return { nullptr, function_stub<function_ptr> };
}
template <class C, R (C::* const method_ptr)(A...)>
static Delegate from(C* const object_ptr) noexcept
{
return { object_ptr, method_stub<C, method_ptr> };
}
template <class C, R (C::* const method_ptr)(A...) const>
static Delegate from(C const* const object_ptr) noexcept
{
return { const_cast<C*>(object_ptr), const_method_stub<C, method_ptr> };
}
template <class C, R (C::* const method_ptr)(A...)>
static Delegate from(C& object) noexcept
{
return { &object, method_stub<C, method_ptr> };
}
template <class C, R (C::* const method_ptr)(A...) const>
static Delegate from(C const& object) noexcept
{
return { const_cast<C*>(&object), const_method_stub<C, method_ptr> };
}
template <typename T>
static Delegate from(T&& f)
{
return ::std::forward<T>(f);
}
static Delegate from(R (* const function_ptr)(A...))
{
return function_ptr;
}
template <class C>
using member_pair =
::std::pair<C* const, R (C::* const)(A...)>;
template <class C>
using const_member_pair =
::std::pair<C const* const, R (C::* const)(A...) const>;
template <class C>
static Delegate from(C* const object_ptr,
R (C::* const method_ptr)(A...))
{
return member_pair<C>(object_ptr, method_ptr);
}
template <class C>
static Delegate from(C const* const object_ptr,
R (C::* const method_ptr)(A...) const)
{
return const_member_pair<C>(object_ptr, method_ptr);
}
template <class C>
static Delegate from(C& object, R (C::* const method_ptr)(A...))
{
return member_pair<C>(&object, method_ptr);
}
template <class C>
static Delegate from(C const& object,
R (C::* const method_ptr)(A...) const)
{
return const_member_pair<C>(&object, method_ptr);
}
void reset() { stub_ptr_ = nullptr; store_.reset(); }
void reset_stub() noexcept { stub_ptr_ = nullptr; }
void swap(Delegate& other) noexcept { ::std::swap(*this, other); }
bool operator==(Delegate const& rhs) const noexcept
{
return (object_ptr_ == rhs.object_ptr_) && (stub_ptr_ == rhs.stub_ptr_);
}
bool operator!=(Delegate const& rhs) const noexcept
{
return !operator==(rhs);
}
bool operator<(Delegate const& rhs) const noexcept
{
return (object_ptr_ < rhs.object_ptr_) ||
((object_ptr_ == rhs.object_ptr_) && (stub_ptr_ < rhs.stub_ptr_));
}
bool operator==(::std::nullptr_t const) const noexcept
{
return !stub_ptr_;
}
bool operator!=(::std::nullptr_t const) const noexcept
{
return stub_ptr_;
}
explicit operator bool() const noexcept { return stub_ptr_; }
R operator()(A... args) const
{
// assert(stub_ptr);
return stub_ptr_(object_ptr_, ::std::forward<A>(args)...);
}
private:
friend struct ::std::hash<Delegate>;
using deleter_type = void (*)(void*);
void* object_ptr_;
stub_ptr_type stub_ptr_{};
deleter_type deleter_;
::std::shared_ptr<void> store_;
::std::size_t store_size_;
template <class T>
static void functor_deleter(void* const p)
{
static_cast<T*>(p)->~T();
operator delete(p);
}
template <class T>
static void deleter_stub(void* const p)
{
static_cast<T*>(p)->~T();
}
template <R (*function_ptr)(A...)>
static R function_stub(void* const, A&&... args)
{
return function_ptr(::std::forward<A>(args)...);
}
template <class C, R (C::*method_ptr)(A...)>
static R method_stub(void* const object_ptr, A&&... args)
{
return (static_cast<C*>(object_ptr)->*method_ptr)(
::std::forward<A>(args)...);
}
template <class C, R (C::*method_ptr)(A...) const>
static R const_method_stub(void* const object_ptr, A&&... args)
{
return (static_cast<C const*>(object_ptr)->*method_ptr)(
::std::forward<A>(args)...);
}
template <typename>
struct is_member_pair : std::false_type { };
template <class C>
struct is_member_pair< ::std::pair<C* const,
R (C::* const)(A...)> > : std::true_type
{
};
template <typename>
struct is_const_member_pair : std::false_type { };
template <class C>
struct is_const_member_pair< ::std::pair<C const* const,
R (C::* const)(A...) const> > : std::true_type
{
};
template <typename T>
static typename ::std::enable_if<
!(is_member_pair<T>{} ||
is_const_member_pair<T>{}),
R
>::type
functor_stub(void* const object_ptr, A&&... args)
{
return (*static_cast<T*>(object_ptr))(::std::forward<A>(args)...);
}
template <typename T>
static typename ::std::enable_if<
is_member_pair<T>{} ||
is_const_member_pair<T>{},
R
>::type
functor_stub(void* const object_ptr, A&&... args)
{
return (static_cast<T*>(object_ptr)->first->*
static_cast<T*>(object_ptr)->second)(::std::forward<A>(args)...);
}
};
namespace std
{
template <typename R, typename ...A>
struct hash<::Delegate<R (A...)> >
{
size_t operator()(::Delegate<R (A...)> const& d) const noexcept
{
auto const seed(hash<void*>()(d.object_ptr_));
return hash<typename ::Delegate<R (A...)>::stub_ptr_type>()(
d.stub_ptr_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
}
#endif // DELEGATE_HPP