CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 42
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    is possible create a any type variable(1 variable that accept any type of values)?

    can i create 1 variable that accept any type?
    and can i give it the NULL value too?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: is possible create a any type variable(1 variable that accept any type of values)

    It depends on what exactly you are trying to do. Take a look at Boost.Any for some inspiration.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    cool.. seems that i can do these:
    Code:
    void Write(void *var1,void *var2=NULL, void *var3=NULL)
    	{
    		cout << var1 << var2 << var3;
    	}
    i just thot do it. and no errors
    thanks

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

    Re: is possible create a any type variable(1 variable that accept any type of values)

    well works fine for a while.. now don't

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: is possible create a any type variable(1 variable that accept any type of values)

    No compile error and "works as desired" isn't the same thing.

    You have discovered that it is possible to print a pointer to void. This is useful when you want to print an address. However, it is not the same thing as printing a "variable that accept any type of values".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by laserlight View Post
    No compile error and "works as desired" isn't the same thing.

    You have discovered that it is possible to print a pointer to void. This is useful when you want to print an address. However, it is not the same thing as printing a "variable that accept any type of values".
    true. that's why works in begining and now don't

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

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by Cambalinho View Post
    can i create 1 variable that accept any type?
    and can i give it the NULL value too?
    Giving a variable a NULL value is really only done for pointers. Via casting, a pointer variable can be specified to point to anything - but this is not really recommended!

    In c++11, an auto variable can be defined to take the type of the result of the RHS of an assignment, but once defined it retains its type whilst in scope.

    See http://msdn.microsoft.com/en-us/library/dd293667.aspx
    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)

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    Giving a variable a NULL value is really only done for pointers. Via casting, a pointer variable can be specified to point to anything - but this is not really recommended!

    In c++11, an auto variable can be defined to take the type of the result of the RHS of an assignment, but once defined it retains its type whilst in scope.

    See http://msdn.microsoft.com/en-us/library/dd293667.aspx
    i found something but isn't compatible with Dev C++

    Code:
    // In header: <boost/any.hpp>
    
    
    class any {
    public:
      // construct/copy/destruct
      any();
      any(const any &);
      any(any &&);//i get several errors inclued these line:(
      template<typename ValueType> any(const ValueType &);
      template<typename ValueType> any(ValueType &&);
      any & operator=(const any &);
      any & operator=(any &&);
      template<typename ValueType> any & operator=(const ValueType &);
      template<typename ValueType> any & operator=(ValueType &&);
      ~any();
    
      // modifiers
      any & swap(any &);
    
      // queries
      bool empty() const;
      const std::type_info & type() const;
    };

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

    Re: is possible create a any type variable(1 variable that accept any type of values)

    i found something but isn't compatible with Dev C++
    As per a previous post, using Dev c++ is not recommended

    http://clicktobegin.net/programming/...dnt-use-dev-c/
    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)

  10. #10
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    As per a previous post, using Dev c++ is not recommended

    http://clicktobegin.net/programming/...dnt-use-dev-c/
    i'm using it, because i need the compiler exe from MinGW32
    if i can use the VS compiler or other IDE it's more easy for me
    Last edited by Cambalinho; August 24th, 2013 at 06:56 AM.

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

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by Cambalinho View Post
    i found something but isn't compatible with Dev C++

    Code:
    // In header: <boost/any.hpp>
    
    
    class any {
    public:
      // construct/copy/destruct
      any();
      any(const any &);
      any(any &&);//i get several errors inclued these line:(
      template<typename ValueType> any(const ValueType &);
      template<typename ValueType> any(ValueType &&);
      any & operator=(const any &);
      any & operator=(any &&);
      template<typename ValueType> any & operator=(const ValueType &);
      template<typename ValueType> any & operator=(ValueType &&);
      ~any();
    
      // modifiers
      any & swap(any &);
    
      // queries
      bool empty() const;
      const std::type_info & type() const;
    };
    That's not what is shown in my copy of boost/any.hpp

    Code:
    any(any &&);
    Is not valid c++! So you woudn't find this in a boost file!
    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)

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    That's not what is shown in my copy of boost/any.hpp

    Code:
    any(any &&);
    Is not valid c++! So you woudn't find this in a boost file!
    i found it: http://www.boost.org/doc/libs/1_54_0/boost/any.hpp
    but can i save it .h?

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

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Code:
    any(any &&);
    is a c++11 rvalue reference feature. If you look at the boost code, you'll see that it's use is guarded by

    Code:
    #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    for when the compiler doesn't support c++11 rvalue references - as dev c++ doesn't.
    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)

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    Code:
    any(any &&);
    is a c++11 rvalue reference feature. If you look at the boost code, you'll see that it's use is guarded by

    Code:
    #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
    for when the compiler doesn't support c++11 rvalue references - as dev c++ doesn't.
    ok..i see.
    but can i convert that temple to a class and use it like a var type?
    see these:

    template<typename Type1> functioname(){}

    can i put in a class for use it normaly like another type?

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by Cambalinho View Post
    i'm using it, because i need the compiler exe from MinGW32
    I've mentioned to you on other threads that the compiler used by Dev-C++ and MingW is what is important. That compiler happens to be gcc.

    The gcc compiler already supports everything that you are looking for.

    http://gcc.gnu.org/projects/cxx0x.html

    MingW also uses gcc to compile projects. Dev-C++ (once again) is an IDE that just happens to have an old version of gcc already plugged into it. If you take a look at the link I gave you above, and do a little searching on the Internet, you should be able to upgrade MingW to use the latest version of gcc.

    The boost libraries are extensively tested with gcc and Visual Studio, and I would say the two platforms that must compile successfully before being accepted by boost.org.

    As to IDE's, there is CodeBlocks, which is up-to-date. Do an Intenet search and you will find other IDE's.

    Regards,

    Paul McKenzie

Page 1 of 3 123 LastLast

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