Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] Raise compilation errors

[Solved] Raise compilation errors

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Max13
    wrote on last edited by
    #1

    Hi,

    Is there a way to simply raise compilation errors?
    For example, in a template class, if I only want to accept pointers type, how can I raise a compilation errors to prevent the user to instantiate the template class with a static type? (Or the contrary)

    I'm looking for doing something like "qMetaTypeId":http://qt-project.org/doc/qt-5/qmetatype.html#qMetaTypeId which raises an error if the type isn't registered, do you guys have an idea?

    Thanks.

    [andreyc: I moved it to C++ Gurus. I think the question is more about C++ then about Desktop development]

    We all have started by asking questions. Then after some time, we can begin answering them.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      If you use C++ 11 then here is an example
      @
      /** http://www.cplusplus.com/reference/type_traits/

      • http://en.cppreference.com/w/cpp/language/static_assert
      • g++ -std=c++11 -o test test.cpp
        */

      #include <iostream>
      #include <type_traits>

      template <class T>
      void test(T a, T b)
      {
      static_assert(std::is_pointer<T>::value, "Test requires pointers");
      auto c = b;
      b = a;
      a = c;
      }

      class A
      {
      public:
      A() {}
      ~A() {}
      };

      int main() {
      std::cout << std::boolalpha;
      std::cout << "is_pointer:" << std::endl;
      std::cout << "int: " << std::is_pointer<int>::value << std::endl;
      std::cout << "int*: " << std::is_pointer<int*>::value << std::endl;
      std::cout << "int**: " << std::is_pointer<int**>::value << std::endl;
      std::cout << "int()(int): " << std::is_pointer<int()(int)>::value << std::endl;

      int* pa = nullptr;
      int* pb = nullptr;
      test(pa, pb);

      A* paA = nullptr;
      A* pbA = nullptr;
      test(paA, pbA);

      A aA;
      A bA;
      test(&aA, &bA);
      test(aA, bA);

      int a = 0;
      int b = 10;
      test(a, b);

      return 0;
      }
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Max13
        wrote on last edited by
        #3

        [quote author="andreyc" date="1401231833"]http://en.cppreference.com/w/cpp/language/static_assert[/quote]

        Oh thanks ! I didn't know about static_assert.

        There also are: Q_STATIC_ASSERT(condition) and Q_STATIC_ASSERT_X(bool, message)

        We all have started by asking questions. Then after some time, we can begin answering them.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved