0

i have a simple function like this in a test.h header

template <class Dst = std::string, class T>
Dst format(const T&);

template <class Dst, Class T>
Dst format(const T&) 
{
 return Dst();
}

in test.cpp

#include "stdafx.h"
#include "test.h"
#include <iostream>
int main(int argc , char** argv)
{
    std::string f = format("");
    std::cout << f;
    return 0;
}

if this header is added to the precompiled header in xcode

the code does not compile any more .

I get a "no matching function call" error.

if i manually add the default parameter to the function call

format<std::string>();

then it works.

if instead of a declaration and a definition i leave only the definition ...it compiles.

Mihai Sebea
  • 398
  • 2
  • 10
  • 1
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of the failing code to show us. And please include the actual errors you get in the question body. – Some programmer dude Jan 20 '17 at 14:03

2 Answers2

1

As I understand, you can't make header precompiled if it contains template, because compiler makes functions from template only when find using of theese functions with concrete types from other sources. No concrete type - no function.

Shadasviar
  • 466
  • 5
  • 15
0

seems that if i put the default parameter in the DEFINITION rather then the declaration it seems to work

template <class Dst , class T>
Dst format(const T&);

template <class Dst = std::string, Class T>
Dst format(const T&) 
{
 return Dst();
}
Mihai Sebea
  • 398
  • 2
  • 10