I wonder if I just use like this about template

template <class T>
class Test
{
....
Test() {createTest();}
using value_type = T;
using class_type = Test<T>;
using iterator = Test<T>*;
....
....
}

template <class T>
void Test<T>::createTest() // How to use 'iterator' instead of Test<T>?
{
.......
}

OR it's fine if I use like that?
And if I have to use 'T' type in createTest, should I use just 'T'? or 'value_type'?
because if I use value_type, there is no 'T' word but I used template <class T>.
is it right thing?
Last edited on
You can try it out yourself, with your own compiler or an online compiler.
e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>

template <class T>
class Test
{
  public:
    using class_type = Test<T>;
    using value_type = T;
    
    value_type thing;
};

int main()
{
    Test<int>  test;
    test.thing = 42;
    std::cout << test.thing << '\n';
}
Last edited on
// Ganado
Thank you for your answer, but I know value_type and T are the same.

What I meant..
I declared template <class T> in createTest function.
and if I use 'value_type' not 'T', you can't see 'T' as a type but you can see 'value_type'
even if I declared 'T' in function. is it fine?
I wonder if users can be confused.

Like this.
1
2
3
4
5
6
7
8
9
10
template <class T>
void Test<T>::createTest()
{
   // both OK.
   value_type temp;
   T temp2;

  // but if I use value_type, you can't see 'T' even if I declared template argument as 'T'
  // What should I use them?
} 
Last edited on
// but if I use value_type, you can't see 'T' even if I declared template argument as 'T'
// What should I use them?


I'm confused.

- What do you mean "if I use value_type"? Use it where?
- What do you mean "you can't see 'T'"?

Please give us 2 code snippets, one showing how T is visible and one showing how T is not visible.
// doug4

I mean, you can use this one
1
2
3
4
5
6
template <class T>
void Test<T>::createTest()
{
   value_type you_can_use; // if i use value_type as a type
   value_type this_type;
}


and this one
1
2
3
4
5
6
template <class T>
void Test<T>::createTest()
{
   T  you_can_use; // if i use T as a type
   T  this_type;
}


I know both codes work the same.
but what I said "see" meaning is just about readability.
If I use value_type as type, a user or whoever can say "where is T used?"
I just want to know which one other people will use if this situation.

I don't speak English well.. so I can't tell you what I mean detail ...
I hope you will get it.. thank you!
Last edited on
This work in C++14

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
#include <iostream>

template <class T>
class Test
{
public:
	using value_type = T;

	T thing;

	T create_value_t();
	T create_value_v();
};


template <class T>
typename Test<T>::value_type Test<T>::create_value_t()
{
	value_type temp = 5;
	return temp;
}

template <class T>
T Test<T>::create_value_v()
{
	T temp = 11;
	return temp;
}

int main()
{
	Test<int> test;
	test.thing = 42;

	std::cout << test.create_value_t() << std::endl;
	std::cout << test.create_value_v() << std::endl;
}
Last edited on
Oh, I think I know what you are asking now.

T and value_type are both names for the same type.

Hopefully you selected "value_type" to be meaningful within the code. It is an alias to the formal type argument to the template. It hides the details of the class implementation from the user so the user does not understand how T fits in.

You can have a class where you have a type that might be something like iterator<BaseClass<T>::CollectionClass>> or something obscure like that. You can create an alias to this with
using MyIterator = iterator<BaseClass<T>::CollectionClass>>;

It is so much easier to just deal with a MyIterator type than the ugly mess that it hides.
Last edited on
That's exactly what I meant. Thanks!
maybe I should study English.. not a cpp
Last edited on
maybe I should study English..


Don't worry about it. I don't always understand what native English speakers are asking, either.
Topic archived. No new replies allowed.