CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: check type

  1. #1
    Join Date
    Feb 2013
    Posts
    22

    check type

    How to check the type of argument passed to each function, checktype, in below?.
    void checktype(void *p)
    {
    }

    or

    template <typename Type>
    Type checktype(Type t) {

    }

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: check type

    In the first case you have some serious flaw in design when you have to check type behind the pointer to void. In C++ this sort of issue is solved by introducing a number of functions having the same name but different prototypes. (However, C++ RTTI might be the way you need)

    In the second case you never need to check the type, as the template causes generation of code for every distinct type you ever use with the template. Of course the generated code does not require any run-time type checking.
    Last edited by Igor Vartanov; February 24th, 2015 at 02:14 AM.
    Best regards,
    Igor

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: check type

    Quote Originally Posted by tcp1 View Post
    How to check the type of argument passed to each function
    C++ offers little but still some support for runtime type checking. It has its uses but shouldn't be overused, especially not to hide design flaws.

    http://en.cppreference.com/w/cpp/language/typeid

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: check type

    Code:
    void checktype(void *p)
    {
    }
    In this example, p is a type of pointer to void. The type of the variable that was used as a parameter to this function when it is used is unknown within the function. IMO variables of type void * should be avoided in c++. If you want a function to be able to be used with different type arguments then either use function overloading or a function template.

    Consider
    Code:
    #include <iostream>
    using namespace std;
    
    void check(void * t)
    {
    	cout << typeid(t).name() << endl;
    }
    
    int main()
    {
    int a;
    
    	check(&a);
    }
    This displays
    Code:
    void *
    as the type of t - not int *.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured