0

Imagine we want to create a UTIL.HPP file specifying some utility functions and a UTIL.CPP to implement them.

Why can't we have in the UTIL.HPP both template functions and regular functions?

The code below doesn't compile (the compiler complains about duplicate symbols):

****************************************************
util.hpp
****************************************************
#ifndef UTIL_HPP
#define UTIL_HPP

template <typename T>
T testMax(T a, T b);

double sum(double a, double b);

#ifndef UTIL_CPP
#include "util.cpp"
#endif

#endif
****************************************************
util.cpp
****************************************************
#ifndef UTIL_CPP
#define UTIL_CPP

#include "util.hpp"

template <typename T>
T testMax(T a, T b) { return (a > b) ? a : b; }

double sum(double a, double b) { return a + b; }

#endif

While the code below does compile (when we don't mix template functions and regular functions):

****************************************************
util.hpp
****************************************************
#ifndef UTIL_HPP
#define UTIL_HPP

template <typename T>
T testMax(T a, T b);

#ifndef UTIL_CPP
#include "util.cpp"
#endif

#endif
****************************************************
util.cpp
****************************************************
#ifndef UTIL_CPP
#define UTIL_CPP

#include "util.hpp"

template <typename T>
T testMax(T a, T b) { return (a > b) ? a : b; }

#endif
Carlos Souza
  • 351
  • 6
  • 13

0 Answers0