[Development] C++11 decltype magic with a container?

Stephen Kelly stephen.kelly at kdab.com
Tue May 21 20:27:41 CEST 2013


Hi,

For a Qt patch, I need to know at compile-time whether std::find can be used 
with it, ie, whether the value_type of the container can be equality-compared.

 https://codereview.qt-project.org/#change,55735

I used a decltype, which works for the type itself, but not for the container. 
I also tried an enable_if, but that doesn't solve the problem either.

Any ideas? The problem can be boiled down to this:


#include <iostream>

struct A {

};

template<typename T, typename = bool>
struct DeclCheck
{
  enum { value = false };
};

template<typename T>
struct DeclCheck<T, decltype(std::declval<T&>() == std::declval<T&>())>
{
  enum { value = true };
};

template<typename T>
struct Container {

  // std::enable_if<DeclCheck<A>::value, bool>::type
  bool
  operator==(const Container other)
  {
    return t == other.t;
  }
private:
  T t;
};

int main()
{
  std::cout << DeclCheck<A>::value << std::endl;
  std::cout << DeclCheck<Container<A>>::value << std::endl;
  return 0;
}


Thanks,

-- 
Stephen Kelly <stephen.kelly at kdab.com> | Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3636 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20130521/ae511259/attachment.bin>


More information about the Development mailing list