Sponsored Content
Top Forums Programming Understanding C++ template partial specialization with pointer datatype arguments Post 302790337 by royalibrahim on Friday 5th of April 2013 07:28:40 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
redi(3) 						     Library Functions Manual							   redi(3)

NAME
redi - All PStreams classes are declared in namespace redi. SYNOPSIS
Classes struct pstreams Common base class providing constants and typenames. class basic_pstreambuf Class template for stream buffer. class pstream_common Class template for common base class. class basic_ipstream Class template for Input PStreams. class basic_opstream Class template for Output PStreams. class basic_pstream Class template for Bidirectional PStreams. class basic_rpstream template for Restricted PStreams. Typedefs typedef basic_pstreambuf< char > pstreambuf Type definition for common template specialisation. typedef basic_ipstream< char > ipstream Type definition for common template specialisation. typedef basic_opstream< char > opstream Type definition for common template specialisation. typedef basic_pstream< char > pstream Type definition for common template specialisation. typedef basic_rpstream< char > rpstream Type definition for common template specialisation. Detailed Description All PStreams classes are declared in namespace redi. Like the standard iostreams, PStreams is a set of class templates, taking a character type and traits type. As with the standard streams they are most likely to be used with char and the default traits type, so typedefs for this most common case are provided. The pstream_common class template is not intended to be used directly, it is used internally to provide the common functionality for the other stream classes. Author Generated automatically by Doxygen for PStreams from the source code. PStreams Thu Nov 10 2011 redi(3)
All times are GMT -4. The time now is 04:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy