template specialization

closed account (oLC9216C)
How can I make a template specialization for pair?

Normally I did :

1
2
3
4
5
6
7
8
9
10
11
12
template<typename X,typename Y>
void temp()
{
pair<X,Y> temp;
}

template<>
void temp()
{
pair<char*,char*> temp;
}


And now I only want one template value

such as

1
2
3
4
5
6
7
8
9
10
11
template<typename X,typename Y>
void temp()
{
pair<X,Y> temp;
}

template<typename X>
void temp()
{
pair<X,char*> temp;
}


How can I do this?
What the problem is? Your code is working fine.
closed account (oLC9216C)
I am working on a template class,

1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename T, typename U>
class temp;
{

template<typename X,typename Y>
friend
istream& operator>>(istream &in, temp<X,Y> &takethis);

private:
pair<T,U> temp;
}



And I want to do this:
1
2
3
4
5
6
7
8
9
10
11
istream& operator>>(istream &in, temp<X,char*> &takethis)
{
}

istream& operator>>(istream &in, temp<char*,Y> &takethis);
{
}

istream& operator>>(istream &in, temp<char*,char*> &takethis)
{
}


I know the code is wrong here, but I don't know what is the correct format to do it.

I want to handle char* differently, but it doesn't let me do it
closed account (oLC9216C)
I also had tried :
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
template<typename T, typename U>
class temp;
{

template<typename X,typename Y>
friend
istream& operator>>(istream &in, temp<X,Y> &takethis);

template<typename X>
friend
istream& operator>>(istream &in, temp<X,char*> &takethis);

template<typename Y>
friend
istream& operator>>(istream &in, temp<char*,Y> &takethis);

private:
pair<T,U> temp;
}
template<typename X,typename Y>
istream& operator>>(istream &in, temp<X,Y> &takethis)
{
}
template<typename X>
istream& operator>>(istream &in, temp<X,char*> &takethis)
{
}
template<typename Y>
istream& operator>>(istream &in, temp<char*,Y> &takethis);
{
}
template<>
istream& operator>>(istream &in, temp<char*,char*> &takethis)
{
}




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
template<typename T, typename U>
class temp;
{

template<typename X,typename Y>
friend
istream& operator>>(istream &in, temp<X,Y> &takethis);

pair<T,U> temp;
}

template<typename X,typename Y>
istream& operator>>(istream &in, temp<X,Y> &takethis)
{
}

template<>
istream& operator>>(istream &in, temp<X,char*> &takethis)
{
}
template<>
istream& operator>>(istream &in, temp<char*,Y> &takethis);
{
}
template<>
istream& operator>>(istream &in, temp<char*,char*> &takethis)
{
}


none of these work
Last edited on
Use partial specialization:
1
2
3
4
5
6
7
8
9
10
template<typename T>
class temp<T, char*>
{
template<typename X>
friend
std::istream& operator>>(std::istream &in, temp<X,char*> &takethis);

private:
std::pair<T, char*> temp;
};
closed account (oLC9216C)
Do you mean this ?

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

template<typename T,typename U>
class temp
{
template<typename X,typename Y>
friend
std::istream& operator>>(std::istream &in, temp<X,Y> &takethis);

private:
std::pair<T, U> temp;
};


template<typename T>
class temp<T, char*>
{
template<typename X>
friend
std::istream& operator>>(std::istream &in, temp<X,char*> &takethis);

private:
std::pair<T, char*> temp;
};


template<typename T>
class temp<T, char*>
{
template<typename X>
friend
std::istream& operator>>(std::istream &in, temp<char*,X> &takethis);

private:
std::pair<char*, T> temp;
};

1. Do not specialise an entire class if all that is required is different behaviour for a single friend function.

2. Avoid specialising function templates. http://www.gotw.ca/publications/mill17.htm

Something like this, perhaps:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <utility>

namespace detail__ { template < typename A, typename B > struct helper ; }

template < typename A, typename B > struct temp
{
    temp( const A& a, const B& b ) : p(a,b) {}

    // ...

    private: std::pair<A,B> p ;

    // ...

    friend std::ostream& operator<< ( std::ostream& stm, const temp<A,B>& t )
    { return detail__::helper<A,B>::help( stm, t ) ; }

    friend detail__::helper<A,B> ; // if required
};

namespace detail__
{
    template < typename A, typename B > struct helper
    {
        static std::ostream& help( std::ostream& stm, const temp<A,B>& )
        { return stm << "generalisation<A,B>" ; }
    };

    template < typename A > struct helper<A,const char*>
    {
        static std::ostream& help( std::ostream& stm, const temp<A,const char*>& )
        { return stm << "specialisation<A,const char*>" ; }
    };

    template < typename B > struct helper<const char*,B>
    {
        static std::ostream& help( std::ostream& stm, const temp<const char*,B>& )
        { return stm << "specialisation<const char*,B>" ; }
    };

    template <> struct helper<const char*,const char*>
    {
        static std::ostream& help( std::ostream& stm, const temp<const char*,const char*>& )
        { return stm << "specialisation<const char*,const char*>" ; }
    };
}

int main()
{
    temp<int,int> a( 1, 2 ) ;
    std::cout << a << '\n' ; // generalisation<A,B>

    temp<int,const char*> b( 1, "abc" ) ;
    std::cout << b << '\n' ; // specialisation<A,const char*>

    temp<const char*,double> c( "efgh", 8.3 ) ;
    std::cout << c << '\n' ; // specialisation<const char*,B>

    temp<const char*,const char*> d( "ijkl", "mnop" ) ;
    std::cout << d << '\n' ; // specialisation<const char*,const char*>
}

http://coliru.stacked-crooked.com/a/53720c2f8adaf90c
Topic archived. No new replies allowed.