Question: I have functions of this type:

```
type uniRndtype()
{
    return typeValue;
}

```

and now I'm trying to wrap them inside another template function like this:

```
template<typename T>
T(* uniRndType(void))()
{
    if (is_same<T, bool>::value)
    {
        return uniRndBool;
    } else if (is_same<T, char>::value)
    {
        return uniRndChar;
    } else
    ...
}

```

and calling it like this:

```
uniRndType<int>();

```

But I'm getting an error: **"error: return value type does not match the function type"** 
because each return has a different type.. 

I there a way to make it work? Because from a runtime point of view I see no errors, only the compiler have problems.

Answer: 
The problem is that while the optimiser can eliminate dead code branches, the front-end (lexical, syntactic & semantic analysis) can't. Which means all code in a template instantiation must be valid. That is, even though in this:

```
if (is_same<T, bool>::value)
{
    return uniRndBool;
}

```

the body will never be executed when `T` is `char`, it must still be valid C++ code. And of course it's not, because `uniRndBool` doesn't have the correct type.

You have two options: a hackish one which works in your particular case, and a generic one.

The hackish one is using `reinterpret_cast<T(*)()>` in all the `return` statements. For the correct `T` brach, it will be a no-op. The other branches will never be executed at runtime, so all will be fine.

The other solution is to use template specialisation. Since it's a [bad idea to specialise function templates](http://www.gotw.ca/publications/mill17.htm), you could use the well-known "delegate to class" trick:

```
template <class T>
struct uniRndTypeHelper;

template <>
struct uniRndTypeHelper<bool>
{
  static bool (*get())() { return uniRndBool; }
};

template <>
struct uniRndTypeHelper<char>
{
  static char (*get())() { return uniRndChar; }
};

template<typename T>
T(* uniRndType(void))()
{
  return uniRndTypeHelper<T>::get();
}

```
```
template <typename T> T (*uniRndType())()
{
    //else
    ...
}

template <> bool (*uniRndType())()
{
    return uniRndBool;
}

template <> char (*uniRndType())()
{
    return uniRndChar;
}

```

That's all.

edit: In principle, we must do like @Angew. but it's little troublesome