Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:22:43+00:00 2026-06-17T08:22:43+00:00

if (std::is_same<T, float>::value) { float a; somefunc_float(x,len,&a); } The above code is from a

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:22:44+00:00Added an answer on June 17, 2026 at 8:22 am

    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 if functionality in a future version of C++ (probably C++17) that would support what you are trying to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code is as follows: #include <cmath> #include <iostream> float foo(float f) { std::cout
From what I understand, if you have, for example, an std::vector<int> and an std::vector<float>
Is there an alternative container that has the same functionality as std::map but it
In standard library, I found that namespace std is declared as a macro. #define
I'm finding that std::sort is very slow with sorting only 1000 items. In class
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
In the following example a std::map structure is filled with 26 values from A
I'm trying to copy a QList in a std::vector this is my code: std::copy(_param_31.listJobs->list_USCOREjobs.begin(),
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
I am trying to understand the following bit of code: #include<iostream> using namespace std;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.