Templates and decltype

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

HalfWayToHell333

Jr.Coder
Silenced
Full Member
Nobleman
Jun 23, 2014
80
783
3
Hey there , here is something that may be of some use.

C++:
template<typename T>
void printVal(T val){
cout << val <<endl;
}

When using this Function , you´ll have to type:

int printme = 4711
printVal<int>(printme);

C++:
template<typename T=decltype(whatever)>//whatever is used as Standart Parameter and used as a Placeholder , the Compiler will do the Rest :) 
void printVal(T val){
cout << val <<endl;
}

you can do this:

int printme = 4711
printVal(printme);
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Learned something new and useful today, thanks! :D
 

HalfWayToHell333

Jr.Coder
Silenced
Full Member
Nobleman
Jun 23, 2014
80
783
3
Learned something new and useful today, thanks! :D
Like we all do ;)

I have revisted my BitsNBytes Source from my other Thread , now signed and unsigned DEC-Val´s are supported.

C++:
#include <iostream>
#include <limits>

using namespace std;

template <typename T = decltype(iDontCare)>
int getBits(T val, int bitPos){
	return (val & (1 << bitPos)) > 0;
	//get val´s bit at leftshiftet bitPos return 1 if >0 else return 0 
	//Credits go to Bit Manipulation from BogotoBogo.com
}

template <typename T = decltype(iDontCare)>
void getInfo(T val, bool minMaxInfo=false, bool signedInfo=false){
	
	int Digits = std::numeric_limits<T>::digits;

	if (sizeof(val) == sizeof(char) || sizeof(val) == sizeof(unsigned char)){

		if (minMaxInfo){
			cout << "Val Min : " << static_cast<int>(std::numeric_limits<T>::min()) << endl;
			cout << "Val Max : " << static_cast<int>(std::numeric_limits<T>::max()) << endl;
		}
		
		if (val < 0) signedInfo = true;

		if (signedInfo){
			cout << "signed  : " << boolalpha << std::numeric_limits<T>::is_signed;
		}
		cout << "\nBits    : " << Digits << endl;

	}
	else{
		if (minMaxInfo){
			cout << "Val Min : " << std::numeric_limits<T>::min() << endl;
			cout << "Val Max : " << std::numeric_limits<T>::max() << endl;
		}
		
		if (val < 0) signedInfo = true;
		
		if (signedInfo){
			cout << "signed  : " << boolalpha << std::numeric_limits<T>::is_signed;
		}
		cout << "\nBits    : " << Digits << endl;
	}

	cout << "setting : ";

	for (int i = Digits - 1; i >= 0; i--){
		cout << getBits(val, i) << " ";
	}

	cout << "\n" << endl;
}


int main()
{

	unsigned char _uchar = 255;//11111111
	char _char = -10;		   //1110110

	unsigned short _ushort = 65535;//1111111111111111
	short _short = -1000;          //111110000011000

	for (int i = 0; i <= 10; i++){
		if (i < 10){
			cout << "val " << i << "   : ";
		}
		if (i >= 10 && i < 100){
			cout << "val " << i << "  : ";
		}
		for (int j = 7; j >= 0; j--){
			cout << getBits(i, j) << " ";
		}

		cout << endl;
	}

	getInfo(_uchar);
	getInfo(_char);

	getInfo(_ushort);
	getInfo(_short);

	cout << "hit enter" << endl;
	cin.get();
	return 0;
}
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,205
78,998
2,400
I still have no idea how to use a template :facepalm:
 

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,747
40,528
317
[GH] Rake;35662 said:
I still have no idea how to use a template :facepalm:
C++:
template <class T>
void Foo(T* Bar)
{
      ReadProcessMemory(hProc, Address, Bar, sizeof(T), nullptr);
}

//Example:

double UrMomma;
Foo<double>(&UrMomma);
Universal ReadProcessMemory :pogchamp:
 

Obsta

Jr.Hacker
Meme Tier VIP
Jan 27, 2014
394
4,338
17
[GH] Rake;35662 said:
I still have no idea how to use a template :facepalm:
It's like, you use a template when you have more than one type of a parameter to pass to the method.

Say i want my function to do some addition, however sometimes you want to add ints, sometimes floats. It gives you choice to specify the type as you call the method. doMath<int>(myInt) or doMath<float>(myFloat)

Hope that helps.
 

HalfWayToHell333

Jr.Coder
Silenced
Full Member
Nobleman
Jun 23, 2014
80
783
3
[GH] Rake;35662 said:
I still have no idea how to use a template :facepalm:
here is an example

C++:
Let´s say you write a add function that returns the result.
Without Templates you´ll have to write one function for every Type (int,float etc.)

int add(int a,int b){
     return a+b;
}

with Templates without decltype you can do this.

template<typename T>
T add(T a,Tb){
return a + b;
}

int a=5;
int b=10;
int result = 0;

calling -> result = add<int>(a,b);

and with decltype you can do this.

template<typename T=delctype(whatever)>//C++ 11!
T add(T a,Tb){
return a + b;
}

calling -> result = add(a,b)

The T replaces the Datatype.
But that is just the Top of the Template Topic.
 

bitm0de

Newbie
Full Member
Feb 28, 2016
19
358
2
Hey there , here is something that may be of some use.

C++:
template<typename T>
void printVal(T val){
cout << val <<endl;
}

When using this Function , you´ll have to type:

int printme = 4711
printVal<int>(printme);

C++:
template<typename T=decltype(whatever)>//whatever is used as Standart Parameter and used as a Placeholder , the Compiler will do the Rest :) 
void printVal(T val){
cout << val <<endl;
}

you can do this:

int printme = 4711
printVal(printme);
You're actually wrong. Explain why you'd need to type out the explicit template type?
C++:
#include <iostream>
#include <string>

template <typename T>
void print(T v)
{
   std::cout << v << std::endl;
}

int main()
{
   int x = 10;
   print(x);
   std::string y("testing");
   print(y);
}
Compiles and works fine for me. Templated function calls are usually pretty good about type deduction. In more advanced cases you can take advantage of type deduction rules as well to avoid decltype too. decltype is useful where there's no context to go by really by usage, but it allows you to define some rules to accurately tell the compiler what you want.
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods