if (std::is_same<T, float>::value)
{
float a;
somefunc_float(x,len,&a);
}
The above code is from a template, which accept a pointer x that can be a pointer of some primitive data type (e.g. x being double *, float * or int *), and somefunc_float is from a lib, can only accept one specific data type of x (float * in the above example), the compiler always give me error, telling me the input data type (x) is incorrect, as if the expressionstd::is_same<T, float>::value doest work at all?
The description of the problem is not completely clear, but I think I understand what you are trying to do: You are enclosing a block of code inside the template function with a test that can be performed at compile time, and expect that the compiler will discard that block and not compile it.
Templates don’t work like that. When a template is instantiated, the whole template is checked and compiled and the code must be correct before the optimizer can discard blocks of code (which it probably would in this case).
The common approach to obtain that behavior is providing multiple implementations of the template (or non-template overloads) that are called with different types. The compiler will dispatch at the place of call to the appropriate implementation and will then ignore the rest.
There are proposals for
static iffunctionality in a future version of C++ (probably C++17) that would support what you are trying to do.