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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:18:44+00:00 2026-06-16T00:18:44+00:00

Considering the following example (I posted it for several different questions today) : #include

  • 0

Considering the following example (I posted it for several different questions today) :

#include <iostream>
#include <vector>
#include <array>
#include <type_traits>

// Version A
template<typename T>
constexpr unsigned int f(const T&)
{
    return 1;
}

// Version B
template<typename... T1, template<typename...> class T>
constexpr unsigned int f(const T<T1...>&)
{
    return 2;
}

// Version C
template<typename T1, template<typename, unsigned int...> class T, unsigned int... N>
constexpr unsigned int f(const T<T1, N...>&)
{
    return 3;
}

// Main
int main(int argc, char* argv[])
{
    std::integral_constant<int, f(double())> a;
    std::integral_constant<int, f(std::vector<double>())> b;
    std::integral_constant<int, f(std::array<double, 3>())> c;
    std::cout<<a<<b<<c<<std::endl; // The goal is to return 123
    return 0;
}

This code does not compile and returns the following compilation error :

temporary of non-literal type 'std::vector<double>' in a constant expression

How to modify this code in order to compile it ?

Note : the goal is to convert a type that will be taken by the first version of the function to 1, a type that will be taken by the second version of the function to 2, etc…

  • 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-16T00:18:44+00:00Added an answer on June 16, 2026 at 12:18 am

    You cannot use temporary vector when defining constant:

    int main(int argc, char* argv[])
    {
        std::integral_constant<int, f(double())> a;
        std::vector<double> vec;
        std::integral_constant<int, f(vec)> b;
        std::integral_constant<int, f(std::array<double, 3>())> c;
    
        std::cout << a << b << c;
        return 0;
    }
    

    The thing is that compiler could possibly omit vector creation, if vector’s only purpose was passing to the constant expression function, but actually it cannot, because vector is not literal type.

    std::array is only a thing wrapper above c array, it has trivial constructor and destructor. As double is also literal type, array of doubles becomes literal.

    Note, however, that if you define

    struct A
    {
        A(){std::cout << "I'm so complicated A!\n"; }
    }
    

    you would not be able to use constructs:

    int main(int argc, char* argv[])
    {
        std::integral_constant<int, f(A())> a;
        std::integral_constant<int, f(std::array<A, 3>())> c;
    
        std::cout << a << b << c;
        return 0;
    }
    

    either, while

    int main(int argc, char* argv[])
    {
        A a_v;
        std::integral_constant<int, f(a_v)> a;
    
        std::array<A, 3> c_v
        std::integral_constant<int, f(c_v)> c;
    
        std::cout << a << b << c;
        return 0;
    }
    

    still would be possible.

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

Sidebar

Related Questions

Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct {
Considering the following data example : data ={ {a, b, c, d, e}, {1,
Considering the following function : template<typename... List> inline unsigned int myFunction(const List&... list) {
Considering the following javascript example: var myobj = { func1: function() { alert(name in
Considering the following example, the line int a = objT + 5; gives ambiguous
Say I have a text file of 1 MB. Considering the following the example:
Considering following code public class A { public static void main(String[] args) { new
Considering the following TSQL: INSERT INTO Address(Street1, City, State, ZipCode) SELECT Street1, City, StateCode,
Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
considering the following enum: public enum LeadStatus { Cold = 1, Warm = 2,

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.