Thread: Implicit functions and subrange types

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

    Implicit functions and subrange types

    I saw a paper somewhere about C++ supporting implicit-callable functions, however compilers like GCC report a warning/error about functions being called implicitly. I think the paper pertained to C++0x, but if that's the case, Wikipedia's article on C++0x as well as GCC's C++0x roadmap don't mention this. I'd like to use this as a substitute for property get/set functionality.


    I ask if implicit functions are usable in C++ at this time and... well, I use GCC, so... if so, does GCC support them?

    Another element that's crossed my mind is if C++ or C support subrange types. Less-used languages such as Pascal or Ada support this and I was wondering if C does too.

    Example of an ICF from what I've read about (C++)
    class Test
    int testvar() implicit {return m_testvar;}; // can be used as getting m_testvar and setting m_testvar, without parentheses, (ie. testvar = 9)
    int m_testvar;
    Example of a subrange type, for reference (Pascal)
    TSubrangeType = 1..10; // only allows a variable of this type to use values between 1 and 10 as well as 1 and 10 themselves, nothing else
    Last edited by Chris87; 05-31-2009 at 08:43 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    I know nothing about what parts of C++0x support in gcc - I'm sure someone else can help there.

    C or C++ does not support "subrange" types. Whilst it is sometimes possible for the compiler to do useful things with subranges, it requires runtime checking to make any meaningful sense. Since C is supposed to be a FAST language, it is not high on the list to make runtime checks.

    If you want to implement a subrange yourself, you could probably make your own template - I'm not too good at template programming, but I'm pretty sure you could make something out of that. You would then, if you like, be able to put a #if DEBUG around any validity checking and capture failure to retain the valid range.

    For some things, you could also use enum to enumerate the values - this obviously isn't very great with simple numbers, but using it for various other things, such as flags, error codes and such would make a lot of sense.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed