Function to "reset" a variable
Dec 2, 2016 at 2:36pm UTC
This is probably a stupid question, I just started C++ like a month ago so forgive me. So basically, I don't know how to create a function that allows me to reset a variable when it's called upon?
1 2 3 4 5 6 7 8 9 10
#include <iostream>
void resetFunction() {
//*something here that lets me change a variable string/int/float (w/e) to like blank, 0 or whatever*//
}
int main() {
int x = 0;
std::string name = "boop" ;
resetFunction(x);
resetFunction(name);
}
Hopefully this makes sense? Thanks in advance.
Dec 2, 2016 at 2:42pm UTC
1 2 3 4
template <typename T>
void reset(T &x){
x = T();
}
Dec 2, 2016 at 2:58pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <typeinfo>
template <typename T>
void resetFunction(T &val)
{
bool isNum =
typeid (val) == typeid (int ) ||
typeid (val) == typeid (float ) ||
typeid (val) == typeid (double ) ||
typeid (val) == typeid (long double ) ||
typeid (val) == typeid (short ) ||
typeid (val) == typeid (long ) ||
typeid (val) == typeid (long long ) ||
typeid (val) == typeid (unsigned ) ||
typeid (val) == typeid (unsigned short ) ||
typeid (val) == typeid (unsigned long ) ||
typeid (val) == typeid (unsigned long long );
bool isString = typeid (val) == typeid (std::string);
bool isChar = typeid (val) == typeid (char ) ||
typeid (val) == typeid (char16_t ) ||
typeid (val) == typeid (char32_t ) ||
typeid (val) == typeid (wchar_t ) ||
typeid (val) == typeid (signed char );
bool isBool = typeid (val) == typeid (bool );
if (isNum)
{
val = 0;
}
else if (isString)
{
val = "" ;
}
else if (isChar)
{
val = '' ;
}
else if (isBool)
{
val = false ;
}
}
Dec 2, 2016 at 3:01pm UTC
I did the above for nothing...
@helios can you tell me how your code works?
Last edited on Dec 2, 2016 at 3:03pm UTC
Dec 2, 2016 at 3:52pm UTC
The default constructor for integers, floats, and pointers returns a zero.
Classes from the standard library default-construct to an "empty" value.
Dec 2, 2016 at 4:11pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <type_traits>
#include <new>
#include <memory>
template < typename T >
typename std::enable_if< std::is_default_constructible<T>::value &&
std::is_move_assignable<T>::value &&
( !std::is_polymorphic<T>::value || std::is_final<T>::value ) >::type
reset( T& object ) { object = T{} ; }
template < typename T >
typename std::enable_if< std::is_default_constructible<T>::value &&
!std::is_move_assignable<T>::value &&
std::is_destructible<T>::value &&
( !std::is_polymorphic<T>::value || std::is_final<T>::value ) >::type
reset( T& object ) { object.~T() ; ::new ( std::addressof(object) ) T{} ; }
Dec 2, 2016 at 6:25pm UTC
boost lexical cast wrote:I did the above for nothing...
... and if you try to instantiate the template it will fail to compile on at least one of the lines 31, 35, 39 or 43.
E.g. resetFunction<std::string> will fail on line 31 and 43 because you can't assign int and bool to a string.
Topic archived. No new replies allowed.