-
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, 07:18 AM
#9
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
#10
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, 06:49 AM
#11
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
#12
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, 11:39 AM
#13
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
-
August 24th, 2013, 05:40 PM
#14
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by Paul McKenzie
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
seems that i didn't understand that before..thanks
1 thing: the CodeBlocks suports C++11?
-
August 24th, 2013, 06:04 PM
#15
Re: is possible create a any type variable(1 variable that accept any type of values)
Originally Posted by Cambalinho
seems that i didn't understand that before..thanks
1 thing: the CodeBlocks suports C++11?
You're still confused.
CodeBlocks is an Integrated Development Environment. It is not a compiler. CodeBlocks, Dev-C++, and even Visual Studio do not know how to compile C++ source code. What does know how to compile C++ source code is the underlying compiler that is used in these programs.
For CodeBlocks, you can basically plug any compiler into it, but most of the time, people configure CodeBlocks to use MingW or gcc. The latest version of gcc is 4.8 (I believe), and it supports all that have been mentioned to you so far.
For Dev-C++, it was designed for gcc 3.x, which is close to 10 years old and out-of-date. Version 3.x of gcc is ok if you're purely interested in only C++ 98 standard, but anything beyond that, 3.x is not going to help you.
For Visual Studio, the compiler used is cl.exe (and cl.exe is being updated to keep up to the current standard (C++ 11). I've heard of Intel compiler being able to be plugged into the Visual Studio IDE, but have never tried it.
Basically what I'm saying to you is that every IDE that I know of uses 2 or 3 executables:
1) The compiler that compiles the source code to object code (gcc.exe, cl.exe, etc.)
2) The linker program that links the various object code (ld.exe, link.exe, etc.)
3) A debugger program or module that allows you to debug the program (gdb.exe, ddd.exe, Visual Studio's integrated debugger, etc.).
Optionally, an IDE may just build a "make" file and call a program that knows how to read the make file and build the program.
Except for 3), where the IDE needs to understand the debugger symbols and position the debugger indicator on the correct line, items 1) and 2) requires just calling the executable that compiles and then links.
As a matter of fact, you don't even need an IDE to develop a program. The reason why IDE's are used is that using the command-line for very large projects becomes very difficult to maintain (unless you have batch files to compile and link large projects, and some people/companies do just that, and that is to create batch files/make files and forget about IDE's to build their programs).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 24th, 2013 at 06:15 PM.
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
|