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

in one of my questions ,somebody said that I have to make sure that
So I have a huge program and decided I should make one of the
I have a sandboxed Applet, but that should not make a difference. The Applet
I have a library base class ( Controller ) and three sub-classes that inherit
I have a class hierarchy where all the implementing classes should have a range
I have very simple query. I want to make sure that I don't have
I am trying to make an application that should help making Dungeon and Dragons
VB.NET 2008 .NET 3.5 I have two base classes that are MustInherit (partial). Let's
I've written a super simple java class that is throwing exceptions as it should.
unit Unit7; interface uses Classes; type TListener = class(TThread) procedure Execute; override; end; TMyClass

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.