Created
November 25, 2017 05:50
-
-
Save t-takata/9426ce77100e504cd0835b1ec6178e55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
template <typename T> T get_additive_identity(T* x) {}; | |
template <> int get_additive_identity(int* x) { return 0;}; | |
template <> double get_additive_identity(double* x) { return 0.0;}; | |
template <typename T> T sum(T* X, int N) { | |
T s = get_additive_identity(X); | |
for (int i = 0; i < N; i++) { | |
s += X[i]; | |
} | |
return s; | |
} | |
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0])) | |
int main(void) { | |
int ai[] = {1,2,3,4,5}; | |
double ad[] = {1,2,3,4,5}; | |
int sumi; | |
double sumd; | |
sumi = sum(ai, ARRAYSIZE(ai)); | |
sumd = sum(ad, ARRAYSIZE(ad)); | |
printf("int: %d\n", sumi); | |
printf("double: %f\n", sumd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment