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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:39:41+00:00 2026-05-16T14:39:41+00:00

It’s my first question there, and it’s a noobish question :). I’m facing to

  • 0

It’s my first question there, and it’s a noobish question :).

I’m facing to a problem with C++ and Qt 4.6, because I want to factorize some of my code which is invoking some public slots of a QObject, through the QMetaMethod::invoke() method.

The problem I’m facing to, is that the Q_ARG macro is defined as follow:

#define Q_ARG(type, data) QArgument<type >(#type, data)

That says that I should know at compile time the type. But I get on the other hand my arguments for the method, which are coming as QVariants. I can get their types through the ->type() accessor, which returns an enum value of type QVariant::Type, but naturally not as compile time type.

So to simply generates the arguments for the invocation, I made the following macro:

#define PASS_SUPPORTED_TYPE(parameterToFill, requiredType, param, supported) {      \
                                            \
        switch (requiredType) {                         \
            case QVariant::String:                      \
                parameterToFill = Q_ARG(QString,            \
                        param.value<QString>());        \
            break;                              \
                                            \
            case QVariant::Int:                         \
                parameterToFill = Q_ARG(int, param.value<int>());   \
            break;                              \
                                                        \
            case QVariant::Double:                      \
                parameterToFill = Q_ARG(double, param.value<double>()); \
            break;                              \
                                                        \
            case QVariant::Char:                        \
                parameterToFill = Q_ARG(char, param.value<char>());     \
            break;                              \
                                                        \
            case QVariant::Bool:                        \
                parameterToFill = Q_ARG(bool, param.value<bool>());     \
            break;                              \
                                                        \
            case QVariant::Url:                         \
                parameterToFill = Q_ARG(QUrl, param.value<QUrl>());     \
            break;                              \
                                                        \
            default:                            \
                supported = false;                  \
                                            \
        }                                   \
                                            \
        supported = true;                           \
}

The same could be done in a method which could return true or false instead of setting the “supported” flag, but this would force me to make heap allocation in this case, because the “param.value()” call returns a copy of the QVariant value, which I should store in heap through a new or through a memset.

And that is my problem, I don’t want to do heap allocation in this method, because this will get called thousands of time (this is a request handling module).

for (int k = 0; k < methodParams.size(); ++k) {
    QVariant::Type paramType = QVariant::nameToType(methodParams[k].toAscii());

    [...]

    bool supportedType = false;

    PASS_SUPPORTED_TYPE(
            paramsToPass[k], 
            paramType, 
            params[k],
            supportedType);
   [...]
}

metaMethod.invoke(objectToCall, paramsToPass[0], paramsToPass[1], paramsToPass[2] [...]);

This does not please me because it’s not type safe. So the question I’m asking myself is how could I fire out this macro, and replace it with a method which would do stack allocation and not heap allocation?

I thank you all in advance for your help and interest.

  • 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-16T14:39:42+00:00Added an answer on May 16, 2026 at 2:39 pm

    And that is my problem, I don’t want
    to do heap allocation in this method,
    because this will get called thousands
    of time (this is a request handling
    module).

    Don’t second guess performances issues. Yes, stack allocation is faster and yes, one should avoid copies when they aren’t needed. However, this looks like premature optimization to me.

    It seems you’re building a very complex code architecture in order to save a few CPU cycles. In the end, you won’t be able to tell reliably what gets called and how many times. And you’ll have an unmaintainable code.

    My advice would be: focus on the correctness and simplicity of your code and if you really face performances issues at some point, profile your code to see what is wrong.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.