-
August 24th, 2013, 05:56 AM
#1
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?
-
August 24th, 2013, 06:00 AM
#2
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.
-
August 24th, 2013, 06:00 AM
#3
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
-
August 24th, 2013, 06:06 AM
#4
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
-
August 24th, 2013, 06:09 AM
#5
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".
-
August 24th, 2013, 06:12 AM
#6
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by laserlight
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
-
August 24th, 2013, 06:18 AM
#7
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by Cambalinho
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)
-
August 24th, 2013, 06:38 AM
#8
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by 2kaud
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;
};
-
August 24th, 2013, 06:49 AM
#9
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)
-
August 24th, 2013, 06:53 AM
#10
Re: is possible create a any type variable(1 variable that accept any type of values)
Last edited by Cambalinho; August 24th, 2013 at 06:56 AM.
-
August 24th, 2013, 07:18 AM
#11
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by Cambalinho
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
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)
-
August 24th, 2013, 07:24 AM
#12
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by 2kaud
That's not what is shown in my copy of boost/any.hpp
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?
-
August 24th, 2013, 07:35 AM
#13
Re: is possible create a any type variable(1 variable that accept any type of values)
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)
-
August 24th, 2013, 07:58 AM
#14
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by 2kaud
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?
-
August 24th, 2013, 11:39 AM
#15
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by Cambalinho
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|