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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:11:24+00:00 2026-05-18T00:11:24+00:00

I have a classes: struct Policy_1 { void signalFailure() { throw std::exception(); } };

  • 0

I have a classes:

    struct Policy_1
    {
        void signalFailure()
        {
            throw std::exception();
        }
    };

    struct Policy_2
    {
        void* signalFailure()
        {
            return nullptr;
        }
    };

    template<class Policy>
    struct My
    {    
        template<class T>
        T* fnc(int value)
        {
            if (!value)
            {
                return Policy::signalFailure();//Here is the problem
            }
            else
            {
                return new int(value);
            }
        }

    };

The problem is that depending on Policy class signalFailure either throws or returns nullptr but I’m getting error from compiler (even though Policy is set that it does throw) that it cannot convert void to (T*) – depending what I’ve substitute for tea; Now, I do understand where the problem lies – compiler does syntax check and discovers that from this Policy, signalFailure returns void so it can’t parse it even though signalFailure in practice throws.

I have two options as I see it:
a) either declare void* as a result type of signalFailure
b) declare void as a result type in fnc and move the result type to a args list of this fnc
or
c) do some (and here is the real Q to you) macro or metaprogramming inside my fnc which would be determined at compile time (either return Policy::etc or just Policy::etc) and that I think would be the best option. Of course if someone of you has better and more elegant idea how to do it, is more than welcome to show it.

  • 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-18T00:11:25+00:00Added an answer on May 18, 2026 at 12:11 am

    I see that you’re using nullptr- which means that you must be in C++0x. In this case it is trivial to use decltype to solve this problem.

    template<class Policy>
    struct My
    {
        template<typename T> struct func_helper {
             static T func() { return Policy::signalFailure(); }
        };    
        template<> struct func_helper<void> {
             static void* func() { Policy::signalFailure(); return nullptr; }
        };
        template<class T>
        T* fnc(int value)
        {
            if (!value)
            {
                return func_helper<decltype(Policy::signalFailure())>::func();
            }
            else
            {
                return new int(value); // Compile error if int* cannot be converted
                                       // to the type of the policy.
            }
        }
    
    };
    

    Edit: Hang on a second. This code is SERIOUSLY wrong. You’re returning a new int(), as T*? This code doesn’t need fixing, it needs the whole module ripping out.

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

Sidebar

Related Questions

I have many classes/methods like this: template<typename CharT, typename TraitsT = std::char_traits<CharT> > struct
I have some classes layed out like this class A { public virtual void
I have two classes, and want to include a static instance of one class
I have multiple classes that all derive from a base class, now some of
I have two classes, Foo and Bar, that have constructors like this: class Foo
We have seen lots of discussion in SO regarding the class vs struct in
I have two vector classes: typedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT
In a C++ program I have two classes (structs) like struct A { int
I have defined a Cloneable interface: struct Cloneable { virtual Cloneable * clone(void) const
I have several classes from 3rd party library similar to the class, StagingConfigDatabase, which

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.