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

Thread: is there a way for test if the class have a function?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,230

    is there a way for test if the class have a function?

    how can i test if the class have a function?
    i did a search but the code don't works(i test several codes without success):
    Code:
    template <typename T>auto has_foo(T& t) -> decltype(t.MouseClick(), bool()) { return true; }
    
    
    bool has_foo(...) { return false; }
    testing:
    Code:
    test(CBase *clsPointer) : atPointer(clsPointer)	{
    		atPointer=clsPointer;
    		std::cout << has_foo(clsPointer);
    	}
    having or don't having, i always get zero.
    so how can i test if the class have a function?

  2. #2
    Join Date
    Feb 2017
    Posts
    343

    Re: is there a way for test if the class have a function?

    Quote Originally Posted by Cambalinho View Post
    how can i test if the class have a function?
    I posted this link in your previous thread already,

    https://en.wikibooks.org/wiki/More_C...ember_Detector

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,411

    Re: is there a way for test if the class have a function?

    Which leads to this for c++17 http://www.open-std.org/jtc1/sc22/wg...2015/n4436.pdf

    Good luck!
    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++17 Compiler: Microsoft VS2017 (15.8.4)

  4. #4
    Join Date
    Apr 2009
    Posts
    1,230

    Re: is there a way for test if the class have a function?

    i'm so sorry to both of you. but, please, see these template:
    Code:
    template <class Type>class TypeHasToString
    {
        // This type won't compile if the second template parameter isn't of type T,
        // so I can put a function pointer type in the first parameter and the function
        // itself in the second thus checking that the function has a specific signature.
        template <typename T, T> struct TypeCheck;
    
    
        typedef char Yes;
        typedef long No;
    
    
        // A helper struct to hold the declaration of the function pointer.
        // Change it if the function signature changes.
        template <typename T> struct ToString
        {
            typedef void (T::*fptr)();
        };
    
    
        template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::MouseClick >*); //function name
        template <typename T> static No  HasToString(...);
    
    
    public:
        static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
    };
    how use it:
    Code:
    int a=TypeHasToString<CBase>::value;
    - 'CBase' it's the class name;
    - 'value' it's the result.
    so what is the problem!?!
    1 - these code, maybe, works like the others... having the same problem: if the class have the function prototype and not defined, i will get '1'(true)... can these be resolved?
    2 - my big problem it's the function name.. unless i can add another template parameter. what you can advice me?
    (i'm sorry something to both, but i'm trying what i can)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)