How to have multiple template parameters?

closed account (Ey80oG1T)
So I have this template:

1
2
3
4
5
template<>
int exampleClass<int>::display()
{
	return variable;
}


But how can I have multiple parameters? Something like this:

1
2
3
4
5
template<>
int exampleClass<int, short, long>::display()
{
	return variable;
}
Have you tried compiling it yet? 😜
closed account (Ey80oG1T)
I did, but it didn't work.
What’s the error?
Actually what’s the code?
closed account (Ey80oG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <typename T>
class exampleClass
{
private:
	T variable;

public:
	exampleClass(T inVariable)
	{
		variable = inVariable;
	}	
        
        int display()
	{
		return variable.length();
	}
};

template <>
int exampleClass<int>::display()
{
	return variable;
}
closed account (Ey80oG1T)
So the method inside the class if for strings, and the one outside the class I want for numbers like int, long, short, etc.
Hum... I’m pretty sure you have to specialize the entire class since the function it’s self isn’t a template function. So you have to do

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
template <typename T>
class exampleClass
{
private:
	T variable;

public:
	exampleClass(T inVariable)
	{
		variable = inVariable;
	}	
        
        int display()
	{
		return variable.length();
	}
};

template <>
class exampleClass <int>
{
private:
	int variable;

public:
	exampleClass(int inVariable)
	{
		variable = inVariable
	}	
        
        int display()
	{
		return variable;
	}
};



Or you could make display() into a template function and then try

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 template <typename T>
class exampleClass
{
private:
	T variable;

public:
	exampleClass(T inVariable)
	{
		variable = inVariable;
	}	
        
        template <class d_t = T>
        int display()
	{
		return variable.length();
	}
};

template <>
int exampleClass::display<int>()
{
	return variable;
}



Edit: I’m pretty sure what I put for the first one isn’t even correct code. The second one might work though, feels a bit hackish though.

Edit2: I sorry if the code doesn’t work I don’t have a compiler rn so I’m kinda flying blind :/
Last edited on
You know, you could just use the example I gave you in your last post:

1
2
3
4
5
6
7
template<typename T>
int display(T value)
{
  std::ostringstream ss{};
  ss << value;
  return ss.str().length();
}


This should work for int, long, std::string, const char*, etc. Furthermore, it also works for classes that have defined overloaded ostream operations. Or wrap it inside your class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 template <typename T>
class exampleClass
{
private:
	T variable;

public:
	exampleClass(T inVariable)
	{
		variable = inVariable;
	}	
        
        int display()
        {
                std::ostringstream ss{};
                ss << variable;
                return ss.str().length();
        }
};

Last edited on
Topic archived. No new replies allowed.