Understanding C++ template partial specialization with pointer datatype arguments


 
Thread Tools Search this Thread
Top Forums Programming Understanding C++ template partial specialization with pointer datatype arguments
# 1  
Old 04-05-2013
Understanding C++ template partial specialization with pointer datatype arguments

When I compile the below code, I am getting error as

Code:
template<typename T> T AddFun(T i, T j) {
    return i + j;
}

template<> T* AddFun<T*>(T* i, T* j) {
    return new T(*i + *j);
}

int main() {
    int n = AddFun<int>(10, 20);
    int i = 10, j = 20;
    int* p = AddFun<int*>(&i, &j);
}

9: error: ‘T’ does not name a type
13: error: expected unqualified-id before ‘int’



If I change the prototype to
Code:
template<typename T> T* AddFun<T*>(T* i, T* j)

then I get the error as 9: error: function template partial specialization ‘AddFun<T*>’ is not allowed.

Kindly help me to correct this problem.
# 2  
Old 04-05-2013
Quote:
Originally Posted by royalibrahim
Code:
template<typename T> T AddFun(T i, T j) {
    return i + j;
}

template<> T* AddFun<T*>(T* i, T* j) {
    return new T(*i + *j);
}

You're trying to make a template specialization, which means you need to use a concrete type instead of the generic T. So you could do e.g.:

Code:
template<> int* AddFun<int*>(int* i, int* j) {
    return new int(*i + *j);
}

(Although you have other issues we won't go into, like why are you trying to add two int pointers?)

The point is, in order to specialize to a pointer type, you have to name the pointer type.

On the other hand, in your first template T can be anything, including a pointer types (which is the whole point of generic programming...) - i.e. you can use any of:

Code:
int a, b;
AddFun<int>(a, b);
AddFun<int *>(&a, &b);
// etc.

On the other hand, you may be trying to say "do this when you're passed a pointer type, do something else when you're passed another type". In this case, you need to revisit all this material as that's not how templates are used - for a start, you probably don't want the return value for your second template to be a pointer (it's just an int) so you want different behaviour - create two differently named templates, one for pointers and one for raw values:

Code:
template<typename T> T AddFun(T i, T j) {
    return i + j;
}

template<typename T> T AddFunP(T* i, T* j) {
    return *i + *j;
}

void foo()
{
    int a, b;
    AddFun<int>(a, b);
    AddFunP<int>(&a, &b);
}


Last edited by JohnGraham; 04-05-2013 at 01:05 PM..
This User Gave Thanks to JohnGraham For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Datatype,structure and dateformat checking.

I have a sourcefile which contains data as below.I want to check whether datatype,structure and date format looks good as mentioned. Data is delemited by cydila Ç. Source file-Emp.txt snoÇnameÇphonenoÇdeptÇjoineddate 1ÇvivekÇ0861ÇCSEÇ2013-05-29 00:00:00 2ÇdineshÇ123456ÇECEÇ2013-05-29 00:00:00... (8 Replies)
Discussion started by: katakamvivek
8 Replies

2. Shell Programming and Scripting

Datatype and length validation

I have sourcefile and structure of source file,i want to check whether datatype and length mention in emp.txt is same as source file. Example: in emp.txt first row contains sno number so in source file also first column should contain only number if data is other than number then that... (1 Reply)
Discussion started by: katakamvivek
1 Replies

3. Shell Programming and Scripting

Datatype file validation

I have a sourcefile which contains data as below.I want to check whether datatype,structure and date format looks good as mentioned. Data is delemited by cydila . Source file-Emp.txt sno name phoneno dept joineddate 1 vivek 0861 CSE 2013-05-29 00:00:00 2 dinesh 123456 ECE ... (2 Replies)
Discussion started by: katakamvivek
2 Replies

4. Shell Programming and Scripting

Help with separating datatype, column name

Hi All, I am new to unix but have a requirement wherein I need to separate datatype,length, and column name from input file which is of below format -- record integer(10) empid; string(25) name; date("YYYY-MM-DD") dob; decimal(10) salary; end now after getting datatype,its length and... (4 Replies)
Discussion started by: phoenix09
4 Replies

5. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

6. Shell Programming and Scripting

AWK - Print partial line/partial field

Hello, this is probably a simple request but I've been toying with it for a while. I have a large list of devices and commands that were run with a script, now I have lines such as: a-router-hostname-C#show ver I want to print everything up to (and excluding) the # and everything after it... (3 Replies)
Discussion started by: ippy98
3 Replies

7. Programming

How to get the size of the datatype passed as the command line argumet?

#include <stdio.h> int main(int argc, char *argv) { printf("%d\n", sizeof(argv)); return 0; } when I run the executable a.out after compiling the above program as: a.out short (or) a.out "long double", I expected to get the output as 2 and 12, but I am always getting the size of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

8. Homework & Coursework Questions

Understanding Functions, sh, and arguments

1. The problem statement, all variables and given/known data: So I have to write a Bourne script that will add two numbers, taking only two arguments and informs the user when it is used incorrectly. 2. Relevant commands, code, scripts, algorithms: It has to be done in sh. I think overall,... (3 Replies)
Discussion started by: lee.n.doan
3 Replies

9. Programming

Help understanding pointer assignment in c

after some years of pause, im returning to c. char *varname = "asd"; int *number = 4; the above code is wrong, because its assigning a value to an unreserved space, or because its changing the address the pointer is pointing ? thanks for the replys!! (3 Replies)
Discussion started by: broli
3 Replies

10. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies
Login or Register to Ask a Question