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

  • SEARCH
  • Home
  • 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 8352235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:54:27+00:00 2026-06-09T08:54:27+00:00

I have a function template that must be allow only certain types. I’ve seen

  • 0

I have a function template that must be allow only certain types. I’ve seen other questions but they used boost and primitve types. In this case, no boost, and it’s a user defined class.

Ex:

template<typename T>
myfunc(T&)
{ ... }

template<>
myfunc(Foo&)
{
   static_assert(false, "You cannot use myfunc with Foo");
}

Problem is static_assert gets called regardless of whether I call myfunc with a Foo object or not.

I just want some way for compile to stop when myfunc is called with Foo.
How can I achieve this functionality?

  • 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-09T08:54:28+00:00Added an answer on June 9, 2026 at 8:54 am

    You can use std::is_same for this:

    #include <type_traits>
    
    template<typename T>
    return_type myfunc(T&)
    {
       static_assert(std::is_same<T, Foo>::value, "You cannot use myfunc with Foo");
    
       // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++, I have a certain template function that, on a given condition, calls
Ok, I have an in template function that get's submitted via method=POST. But I
I have a template class with a variadic template member function that I am
I have to implement a function that takes an iterator. The iterator must dereference
I thought that I wanted to template function specialization, but this stackoverflow article makes
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Suppose I have a static function template template<int I> void ft() within a struct
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename
Say I have a template function in namespace A. I also have another namespace

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.