Thread: Concepts substitute/emulation for C++ (17)?

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    131

    Concepts substitute/emulation for C++ (17)?

    With concepts not coming until C++20 and otherwise being exclusive to GCC, is there a way to sort of workaround I can do? I did see some old pre-C++11 how-tos but there was a 'to do' placeholder for static_assert, in regards for template-based concept workarounds.

    Basically, I want to define a concept that represents a container (ex: standard library's string, vector, array, map classes, and so on). With the current draft for concepts, I could just define the Container concept with "requires push_back(), front(), last(), etc" willy nilly. I do know there's a way to sort of do this without concepts, but my searches haven't yielded any suitable answers.

  2. #2
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    131
    Think I found my answer just now after a bit more digging in Boost's concept check component. Including boost/concept_check.hpp provides me with a few options, including Container (Container )

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Look up void_t.
    The error messages will be tricky if something goes wrong, but you can emulate concepts pretty well.
    std::void_t - cppreference.com
    Last edited by manasij7479; 11-20-2017 at 09:38 AM.

  4. #4
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    131
    Seems confusing, but I see some relation to concepts as far as that goes. Thanks.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Essentially what you can do is:
    Code:
    // Define is_containter<T> with SFINAE/void_t/magic
    
    template<typename T>
    void foo(T t) {
      static_assert(is_container<T>::value, "requires is_container");
      // ...
    }

  6. #6
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    131
    Quote Originally Posted by manasij7479 View Post
    Essentially what you can do is:
    Code:
    // Define is_containter<T> with SFINAE/void_t/magic
    
    template<typename T>
    void foo(T t) {
      static_assert(is_container<T>::value, "requires is_container");
      // ...
    }
    Yep! I guess that works too, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Substitute string with file
    By mmk1234 in forum C Programming
    Replies: 4
    Last Post: 01-20-2015, 06:03 PM
  2. input asteriks to substitute characters
    By eastgod in forum C Programming
    Replies: 2
    Last Post: 12-10-2012, 08:37 AM
  3. any substitute for conio.h(turbo c++) in g++ compiler?
    By aditya1 in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2005, 04:10 PM
  4. emulation...
    By deathstryke in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 11-25-2002, 08:23 AM
  5. Emulation
    By Generator in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2001, 03:44 PM

Tags for this Thread