Function Template Headers Causing Error

How come when I try to compile this with G++ it fails:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

template <typename T>
inline const& T printSomething(T a)
{
  std::cout << a << std::endl;
}


int main()
{
  ::printSomething("Hello");
}


1
2
3
4
5
6
max4.cpp:4:17: error: expected initializer before ‘printSomething’
 inline const& T printSomething(T a)
                 ^
max4.cpp: In function ‘int main()’:
max4.cpp:13:3: error: ‘::printSomething’ has not been declared
   ::printSomething("Hello");


But if I simply switch the function header around to

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

template <typename T>
inline T const& printSomething(T a)
{
  std::cout << a << std::endl;
}


int main()
{
  ::printSomething("Hello");
}


It works? (Notice I just switched the header of the printSomething function; "T" switched with "const&")
Last edited on
Because the first is wrong syntax while the second is correct.
If we were using concrete types, we could write the function like the aforementioned first example. For instance,

1
2
3
4
inline const& int printSomething(int a)
{
   std::cout << a << std::endl;
}


This above function is simply returning a constant reference to an int. Why doesn't that same syntactical logic apply for template functions?
That shouldn't compile, does it for you?
a constant reference to an int would be
const int& or int const&.
Last edited on
AHAHAHAHAHAHAHA! It's all becoming clear now, Ganado. Thank you. That was helpful. It was just a stupid misplaced &. . .
Topic archived. No new replies allowed.