Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ struct template

I tried to use templates and structs, but they don't work. I searched a lot, but I can't find the solution.

#include <iostream>

using namespace std;

template<struct S>
int add(S s) {
    return s.num + s.num2;
}

int main() {

    struct {
        int num = 10;
        int num2 = 20;
    } test;

    cout << add(test) << endl;
    return 0;
}

With gcc the errors are:

test.cpp:5:17: error: ‘struct S’ is not a valid type for a template non-type parameter
test.cpp: In function ‘int add(S)’:
test.cpp:6:5: error: ‘s’ has incomplete type
test.cpp:5:17: error: forward declaration of ‘struct S’
test.cpp: In function ‘int main()’:
test.cpp:13:19: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
test.cpp:14:20: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
test.cpp:17:21: error: no matching function for call to ‘add(main()::<anonymous struct>&)’
test.cpp:17:21: note: candidate is:
test.cpp:6:5: note: template<<declaration error> > int add(S)
test.cpp:6:5: note:   template argument deduction/substitution failed:
test.cpp:17:21: note:   cannot convert ‘test’ (type ‘main()::<anonymous struct>’) to type ‘S’
like image 417
Hard Rain Avatar asked Nov 26 '12 15:11

Hard Rain


People also ask

Can you template a struct?

You can template a struct as well as a class. However you can't template a typedef. So template<typename T> struct array {...}; works, but template<typename T> typedef struct {...}

Can you make templates in C?

In C, the only native means of creating static templates are through the use of macros. printf("%d", foo); The DEF macro described above can work with any type which can be initialized via the = operator.

What does template <> mean in C++?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Is template in C Plus Plus?

A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.


2 Answers

You can't use the keyword struct here. Use class or typename (you can still instantiate your template with a struct though).

EDIT: Your other problems are to do with the anonymous struct in main. Can't remember the rules off the top of my head (perhaps someone else can explain them), but this is a pretty weird way of doing things anyway, so I just offer a working version: http://ideone.com/VGIogH

Somewhat relevant: Can we have an anonymous struct as template argument?

EDIT AGAIN: What you originally wrote, with the anonymous struct in main works (after replacing struct in the template) with --std=c++11, but not without (i.e. c++03).

like image 64
BoBTFish Avatar answered Nov 09 '22 23:11

BoBTFish


In C++ a struct and a class are basically the same thing, except that the default access specifier is public in the former and private in the latter. On the other hand, the syntax for defining a template type argument requires the use of either the class or typename keywords (Don't confuse class here with a class in the OO sense, it can be any type).

template <typename S>          // equivalently class S
int add(S s) ...
like image 20
David Rodríguez - dribeas Avatar answered Nov 09 '22 23:11

David Rodríguez - dribeas