Difference between revisions of "CPP/Templates/FunctionTemplateHeaderOnly"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==FunctionTemplate.cpp== <source lang="cpp"> #include <iostream> template<typename T> void PrintSomething(T something); int main(int, char*[]) { double a = 1.2; PrintSometh…')
 
Line 1: Line 1:
==FunctionTemplate.cpp==
+
==FunctionTemplateHeaderOnly.cpp==
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <iostream>
 
#include <iostream>
  
template<typename T>
+
#include "Point.h"
void PrintSomething(T something);
+
  
 
int main(int, char*[])
 
int main(int, char*[])
 
{
 
{
  double a = 1.2;
+
   PrintSomething(1.0);
   PrintSomething(a);
+
  
 
   // Sometimes have to do this:
 
   // Sometimes have to do this:
   PrintSomething<double>(a);
+
   PrintSomething<double>(1.0);
  
 
   return 0;
 
   return 0;
 
}
 
}
 +
 +
</source>
 +
 +
==Point.h==
 +
<source lang="cpp">
  
 
template<typename T>
 
template<typename T>
Line 22: Line 25:
 
   std::cout << something << std::endl;
 
   std::cout << something << std::endl;
 
}
 
}
 
template void PrintSomething<double>(double);
 
  
 
</source>
 
</source>
Line 31: Line 32:
 
cmake_minimum_required(VERSION 2.6)
 
cmake_minimum_required(VERSION 2.6)
  
Project(FunctionTemplate)
+
Project(FunctionTemplateHeaderOnly)
  
ADD_EXECUTABLE(FunctionTemplate FunctionTemplate.cpp)
+
ADD_EXECUTABLE(FunctionTemplateHeaderOnly FunctionTemplateHeaderOnly.cpp)
  
 
</source>
 
</source>

Revision as of 22:43, 29 January 2011

FunctionTemplateHeaderOnly.cpp

#include <iostream>
 
#include "Point.h"
 
int main(int, char*[])
{
  PrintSomething(1.0);
 
  // Sometimes have to do this:
  PrintSomething<double>(1.0);
 
  return 0;
}

Point.h

template<typename T>
void PrintSomething(T something)
{
  std::cout << something << std::endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(FunctionTemplateHeaderOnly)
 
ADD_EXECUTABLE(FunctionTemplateHeaderOnly FunctionTemplateHeaderOnly.cpp)