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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:07:06+00:00 2026-06-05T10:07:06+00:00

I’m having trouble with auto and decltype. void f(const vector<int>& a, vector<float>& b) {

  • 0

I’m having trouble with auto and decltype.

void f(const vector<int>& a, vector<float>& b)
{
    typedef decltype(a[0]*b[0]) Tmp;
    for (int i=0; i < b.size(); ++i) {
      auto p0 = new auto(a[i]*b[i]);
      auto p1 = new decltype(a[i]*b[i]);
      *p0=a[i]*b[i];
      *p1=a[i]*b[i];
      cout<<*p0<<endl;
      cout<<*p1<<endl;
      delete p0;
      delete p1;
   }
}

 int main()
{

    vector<float>vec2;
    vec2.push_back(2.0);

    vector<int>vec1;
    vec1.push_back(5);

    return 0;
}

The above code runs well in GCC4.7. Could I use ‘new auto(a[0]*b[0])’ to allocate memory for the type of a[0]*b[0]? And I can not distinguish the difference between decltype and auto in this case.

  • 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-05T10:07:09+00:00Added an answer on June 5, 2026 at 10:07 am

    The difference is that this:

      new auto(a[i]*b[i]);
    

    is allocating an object of whatever type a[i]*b[i] is, and initializes the object with that value. That is, the parentheses are an initializer. Whereas using decltype:

      new decltype(a[i]*b[i]);
    

    allocates an object of the same type, but there is no initializer. The object is default initialized.

    Basically decltype(...) is treated as a type, while auto specifies a type to be deduced from an intializer.


    C++11 Style

    Since new shouldn’t be used except in special cases, if for some reason these semantics are needed they’d properly be written something like this:

    template<typename T, typename... Args> T make_unique(Args &&...args) {
      return std::unique_ptr<T>{std::forward<Args>(args)...};
    }
    
    template<typename T> T make_unique_auto(T &&t) {
        return std::unique_ptr<T>{std::forward<T>(t)};
    }
    
    // new auto(a[i]*b[i])
    auto p1 = make_unique_auto(a[i]*b[i]);
    
    // new decltype(a[i]*b[i])
    auto p2 = make_unique<decltype(a[i]*b[i])>();
    

    Furthermore, if one gets used to using C++11 uniform initialization universally the and if one stops using parentheses for initialization then eventually the parentheses used with decltype cease to look to the programmer like an initializer.

    For this reason and others, I think it would be good for a ‘modern’ C++11 style to include a rule that one should always use braces and never use parentheses for initialization. (And constructors that cannot be called except with parentheses, e.g. std::vector<int>(int,int);, should be avoided; don’t create new ones and don’t use legacy ones.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.