English 中文(简体)
SFINAE 查明非成员的职能存在
原标题:SFINAE To detect non-member function existence
  • 时间:2010-07-30 21:56:57
  •  标签:
  • c++
  • sfinae

是否有任何人知道根据是否界定了非成员方法而专门使用模板的方法? 我知道,如果存在成员的职能,就有许多专业方法,但我从未看到过非成员的例子。 具体的问题是:专门处理运营商和提炼;共享商适用运营商和提炼;如果运营商和提炼;对T进行定义,并另外印刷点位置。 如果所有班级都界定了运营商的配制;加之;作为成员,但不幸的是,许多人使用自由职能。 我想象像以下东西:

template <typename T>
typename enable_if< ??? ,std::ostream &>::type operator<<( std::ostream & os, const shared_ptr<T> & ptr )
{
  if(ptr)
   return os << *ptr;
  else
   return os << "<NULL>";
}

template <typename T>
typename disable_if< ??? ,std::ostream &>::type operator<<( std::ostream & os, const shared_ptr<T> & ptr )
{
  if(ptr)
   return os << static_cast<intptr_t>( ptr.get() );
  else
   return os << "<NULL>";
}

Edit:就邮政而言,这里是工作解决办法。 注:加固:共享的吸收器已经安装了缺损操作器;加固;投放地址,因此没有必要使用。 由于运营商和提炼;回报是参考,因此这项工作。 就一般情况而言,我怀疑这必须加以调整,以反映有关职能的返回类型。

template <typename T>
typename boost::enable_if_c< boost::is_reference<decltype(*static_cast<std::ostream *>(0) << *static_cast<T *>(0) )>::value, std::ostream &>::type operator<<( std::ostream & os, const boost::shared_ptr<T> & ptr )
{
  if(ptr)
   return os << *ptr;
  else
   return os << "<NULL>";
}
最佳回答

如果你使用C++0x,那么你就可以简单地使用分离式。

template<typename Char, typename CharTraits, typename T>
        decltype(
            *(std::basic_ostream<Char, CharTraits>*)(nullptr) << *(T*)(nullptr)
        )

如果T无法生产,这无疑会造成替代失败。 在C++03中,你可能做一些类似的事情,但我不敢肯定。

Edit:正义地认识到,过时的表达实际上会产生真实或虚假的价值,并赢得汇编。 但是,你们看到了情况。 引证。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签