Thread: compiler-time checker question.

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    compiler-time checker question.

    I have this piece of code from the book "Modern C++ Design" that checks for compile-time error. When i tried to compile it, i get the error "invalid application of sizeof to function type".

    Could someone give me a pointer how to make this compiler-time checker work?

    Code:
    template<bool> struct CompileTimeChecker{
      CompileTimeChecker(...);
    };
    
    template<> struct CompileTimeChecker<false> {};
    
    #define STATIC_CHECK(expr,msg)\
    {\
      class ERROR_##msg {};\
      (void)sizeof(CompileTimeChecker<(expr)!=0>((ERROR_##msg())));\
    }
    
    template<class To, class From>
    To safe_reinterpret_cast(From from)
    {
      STATIC_CHECK(sizeof(From) <= sizeof(To),Destination_type_too_narrow);
      return reinterpret_cast<To>(from);
    }
    
    int main(){
      void * myptr = new void;
      char c = safe_reinterpret_cast<char>(myptr);
    }
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    sizeof(CompileTimeChecker<(expr)!=0>((ERROR_##msg())));
    O_o

    Code:
    sizeof((CompileTimeChecker<(expr)!=0>((ERROR_##msg()))));
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>void * myptr = new void;
    You cannot allocate a void type (how do the compiler know how many bytes it should allocates?).

    What's this compile-time checker supposed to be anyway?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by phantomotap View Post
    Code:
    sizeof(CompileTimeChecker<(expr)!=0>((ERROR_##msg())));
    O_o

    Code:
    sizeof((CompileTimeChecker<(expr)!=0>((ERROR_##msg()))));
    Soma

    Could you explain why that needs another parentheses pair?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Could you explain why that needs another parentheses pair?
    O_o

    I don't think I can without going to some extremely technical places.

    I think can show you without much difficulty.

    Code:
    int main()
    {
        {
            int s1(); /*!*/ // Declare a function taking nothing and returning an `int' variable.
            // s1 = 0; // Nope. The above is not a variable definition/declaration.
        }
        {
            int s2(int); /*!*/ // Declare a function taking an `int' variable and returning an `int' variable.
            // s2 = 0; // Nope. The above is not a variable definition/declaration.
        }
        {
            int s3(int()); /*!*/ // Declare a function taking a function pointer (The function provided would need to be a function taking nothing and returning an `int' variable.) variable and returning an `int' variable.
            // s3 = 0; // Nope. The above is not a variable definition/declaration.
        }
        {
            int s4((int())); // Declare a variable of `int' type and initialize that variable to the same variable as a default constructed variable of `int' type.
            s4 = 0; // Fine. The above is a variable definition/declaration. The extra parentheses prevent the call, the default `int' constructor is called, from looking like a declaration.
        }
        {
            // sizeof(int()); // Nope. The `sizeof' operator can not be used on the result of calling a function. As I describe above, the `int()' expression is a declaration.
        }
        {
            sizeof((int())); // Fine. The `sizeof' operator can be used on the result of calling a function. (The default constructor for `int' type is being called.) As I describe above, the extra parentheses prevent the call from looking like a declaration.
        }
    }
    The expression in your code is complex, but the underlying cause of the error is essentially the same as the errors shown in my example.

    I'd like to give a simple explanation, but you need to keep in mind that I am glossing over a lot.

    The error lines in my example look like they might be a label declaration; the standard says that anything that looks like a declaration is a declaration.

    [Edit]
    I've marked the relevant problem areas, from your perspective, with a '!' character.
    [/Edit]

    *shrug*

    Yes. You find yourself in an odd corner of the C++ language.

    Soma
    Last edited by phantomotap; 11-30-2014 at 11:59 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spell Checker need help
    By panosstylianou in forum C Programming
    Replies: 55
    Last Post: 11-16-2011, 12:07 PM
  2. Password Checker
    By C_ntua in forum C++ Programming
    Replies: 22
    Last Post: 09-23-2008, 04:24 PM
  3. URL checker
    By bigSteve in forum C++ Programming
    Replies: 10
    Last Post: 05-24-2004, 04:48 PM
  4. spell checker in c needs help
    By madmax in forum C Programming
    Replies: 3
    Last Post: 03-13-2003, 09:36 AM
  5. HTML Tag Checker
    By Steven Snell in forum C Programming
    Replies: 2
    Last Post: 03-22-2002, 07:26 PM