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 3352442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:01:50+00:00 2026-05-18T02:01:50+00:00

How I should make sure that classes needed as a one of params to

  • 0

How I should make sure that classes needed as a one of params to my templated class will have a certain interface? I know that one way is to make in Interface class pure virtual but I would like to avoid it. Is there any other way to do it? I would like to avoid anything which isn’t standard

    //Example  
    template<class SomePolicy>  
    class My
    {
void fnc()const
{
SomePolicy::mustHaveThisInterface();//<--here I have to have  
// this interface in orded to work

}
    };
  • 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-05-18T02:01:51+00:00Added an answer on May 18, 2026 at 2:01 am

    A simple solution is to have a dummy function that validates the requirement. Example:

    class Interface {};
    
        // Basically a no-op, but has to compile.
    template<typename I, typename T>
    void require ()
    {
        T * t = 0; I * i = t;
    }
    
    template<class SomePolicy>
    class My
    {
        void fnc () const
        {
                // won't compile unless `SomePolicy` inherits `Interface`.
            require<Interface, SomePolicy>();
        }
    };
    

    Note that, since this is a template, My<T>::fnc() won’t be compiled unless it is called, which means your validation require<I,T>() directive must be placed somewhere important (e.g. the constructor, or at the beginning of each function where SomePolicy must inherit Interface.) Just how many checks you put depends on your level of paranoia.

    Performance Note: since the check amounts to a no-op, any decent compiler will optimize it out so it won’t cost anything in run time or memory. The compiler is still obliged to make it compiile though, which means it can’t skip the test at compile-time.

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

Sidebar

Related Questions

Some people have suggested that when doing an estimate one should make a lower
A sysadmin teacher told me one day that I should learn to use make
Should I make my own framework by wrapping up the STL classes and/or Boost
When should I continue to make derived classes, and when should I just add
I have a class A providing Bitmaps to other classes B, C, etc. Now
I am building a search box (input field) which should make a server call
What CSS should I use to make a cell's border appear even if the
If I make two iPhone applications, how can/should I share custom data (not contacts
I want to make the line marked with // THIS LINE SHOULD BE PRINTING
What do you think - which functions should be implemented to make work with

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.