Okay, let's break this down.
First, let's make some assumptions to have complete, compilable code. I'll assume Wrapper might also want sizeP or AAA for some reason, and Request needs sizeP.
#include <vector>
#include <string>
#include <iostream>
#include <type_traits> // For is_same_v
// --- Definitions to make the code compile ---
template<size_t S>
struct Request {
char data[S]; // Just some data
};
template<typename T_Wrapper, size_t S_Wrapper = 0> // S_Wrapper can be optional or tied to Response
struct Wrapper {
T_Wrapper value;
// size_t size_info = S_Wrapper; // Potentially use S_Wrapper
};
// Your Response alias
template<typename T_Response, size_t sizeP_Response> // I'm adding sizeP_Response here to match usage
using Response = std::vector<Wrapper<T_Response, sizeP_Response>>;
// --- End Definitions ---
// Your original functions
template <typename AAA, size_t sizeP>
void vvvvWWWW(const Request<sizeP>& request) {
std::cout << "vvvvWWWW called.\n";
std::cout << " AAA is: " << typeid(AAA).name() << std::endl;
std::cout << " sizeP is: " << sizeP << std::endl;
// If AAA was MyType, you might do something like:
// Wrapper<AAA, sizeP> temp_wrapper;
// If AAA was Wrapper<MyType, sizeP>, this is different.
}
template <typename AAA, size_t sizeP>
void xxxxQQQQ(const Request<sizeP>& request,
Response<AAA, sizeP>& response_arg) { // response_arg is std::vector<Wrapper<AAA,sizeP>>&
std::cout << "xxxxQQQQ called.\n";
std::cout << " AAA is: " << typeid(AAA).name() << std::endl;
std::cout << " sizeP is: " << sizeP << std::endl;
// Here, AAA is the T_Response from Response<T_Response, sizeP_Response>
// So if response_arg is std::vector<Wrapper<MyType, 10>>, then AAA is MyType and sizeP is 10.
}
Now let's analyze the calls:
int main() {
Request<10> request_obj; // sizeP for request_obj is 10
// Define a response object. Let's say we want AAA to be 'int'
// and sizeP to be 10 (matching request_obj for consistency)
Response<int, 10> response_obj; // This is std::vector<Wrapper<int, 10>>
std::cout << "--- Calling vvvvWWWW ---" << std::endl;
// Your call: vvvvWWWW<decltype(response_obj)::value_type>(request_obj);
// Let's break down decltype(response_obj)::value_type:
// 1. decltype(response_obj) is Response<int, 10>, which is std::vector<Wrapper<int, 10>>
// 2. ::value_type of std::vector<Wrapper<int, 10>> is Wrapper<int, 10>
// So, you are explicitly instantiating vvvvWWWW with AAA = Wrapper<int, 10>
// and sizeP = 10 (deduced from request_obj)
vvvvWWWW<decltype(response_obj)::value_type, 10>(request_obj); // sizeP must be specified or deducible from Request
// Corrected to provide sizeP as well.
std::cout << "\n--- Calling xxxxQQQQ ---" << std::endl;
// Your call: xxxxQQQQ(request_obj, response_obj);
// 1. request_obj is Request<10>.
// 2. response_obj is Response<int, 10>&, which is std::vector<Wrapper<int, 10>>&
// Template argument deduction for xxxxQQQQ:
// - From request_obj (type Request<sizeP>), sizeP is deduced as 10.
// - From response_obj (type Response<AAA, sizeP>&), AAA is deduced as int, and sizeP is deduced as 10.
// The deductions for sizeP must be consistent.
xxxxQQQQ(request_obj, response_obj);
return 0;
}
Why vvvvWWWW<decltype(response)::value_type>(request) is not same like xxxxQQQQ(request, response) but both are compiling?
-
Different AAA types:
- For
vvvvWWWW<decltype(response_obj)::value_type, 10>(request_obj):
decltype(response_obj) is std::vector<Wrapper<int, 10>>.
decltype(response_obj)::value_type is Wrapper<int, 10>.
- So,
AAA is explicitly specified as Wrapper<int, 10>.
sizeP is specified as 10.
- For
xxxxQQQQ(request_obj, response_obj):
response_obj is of type Response<int, 10>& (which is std::vector<Wrapper<int, 10>>&).
- The template parameter
AAA in Response<AAA, sizeP>& is deduced by matching with int.
- So,
AAA is deduced as int.
sizeP is deduced as 10.
The crucial difference is that in the vvvvWWWW call, AAA becomes the Wrapper type itself, while in xxxxQQQQ, AAA becomes the type inside the Wrapper (the T_Response template argument to Response).
-
How AAA is determined:
- In
vvvvWWWW, AAA cannot be deduced from its arguments because it doesn't appear in the type of request. You must specify it explicitly. You chose to specify it as decltype(response)::value_type.
- In
xxxxQQQQ, AAA is part of the type of the response_arg parameter (Response<AAA, sizeP>&), so it can be deduced by the compiler.
-
Compilation:
Both compile because they are valid template instantiations:
vvvvWWWW<Wrapper<int, 10>, 10>(const Request<10>&) is a valid instantiation.
xxxxQQQQ<int, 10>(const Request<10>&, std::vector<Wrapper<int, 10>>&) is also a valid instantiation.
They just mean different things for AAA.
If you wanted vvvvWWWW to have AAA be int (like in xxxxQQQQ), you'd need a way to extract int from Response<int, 10>.
You could use a type trait:
template<typename T>
struct extract_inner_type; // Primary template
// Specialization for your Response type
template<typename T_AAA, size_t T_sizeP>
struct extract_inner_type<Response<T_AAA, T_sizeP>> {
using type = T_AAA;
static constexpr size_t size = T_sizeP;
};
// ... in main ...
std::cout << "\n--- Calling vvvvWWWW with extracted type ---" << std::endl;
// AAA will be int, sizeP will be 10
using ExtractedAAA = typename extract_inner_type<decltype(response_obj)>::type;
constexpr size_t ExtractedSizeP = extract_inner_type<decltype(response_obj)>::size;
vvvvWWWW<ExtractedAAA, ExtractedSizeP>(request_obj);
This call would make AAA in vvvvWWWW be int and sizeP be 10, matching xxxxQQQQ.
CAN WE DO SOMETHING LIKE THIS IN C++20?
template <typename class<typename AAA, size_t> Proxy, size_t sizeP> void vvvvWWWW(const Request<sizeP>& request)
This syntax is for template template parameters, but it's not quite right and doesn't achieve what you seem to intend for deducing AAA within vvvvWWWW without passing an instance of Proxy.
A template template parameter allows you to pass a template name (like std::vector, or your Response if it were a class template rather than an alias) as an argument.
Corrected syntax for a template template parameter:
template <template<typename, size_t> class Proxy, /* other params */>
Let's analyze your intent:
You want vvvvWWWW to somehow "know" about AAA and sizeP that are associated with a Proxy type (like Response), without actually passing an instance of Response<AAA, sizeP>.
The template <typename class<typename AAA, size_t> Proxy...> syntax you proposed is trying to destructure the template arguments of Proxy itself within the template parameter list of vvvvWWWW. This is not how template template parameters work. typename AAA, size_t inside class<...> are placeholders describing the signature of the template that Proxy must match; they don't become usable types/values AAA and (a non-type) sizeP for vvvvWWWW directly.
What template template parameters can do:
template <typename T_Actual, size_t S_Actual> // Actual types for the Proxy
struct MyConcreteProxy {
T_Actual data;
};
// TT_Proxy is a template that takes a type and a size_t
// e.g., MyConcreteProxy could be passed for TT_Proxy
template <template<typename, size_t> class TT_Proxy,
typename ActualTypeForProxy, size_t ActualSizeForProxy, /* for instantiation */
size_t requestSizeP>
void some_func(const Request<requestSizeP>& req) {
// We can now use TT_Proxy to instantiate a type:
TT_Proxy<ActualTypeForProxy, ActualSizeForProxy> instance;
// instance has type MyConcreteProxy<ActualTypeForProxy, ActualSizeForProxy>
// if TT_Proxy was MyConcreteProxy.
std::cout << "some_func with TT_Proxy called.\n";
std::cout << " ActualTypeForProxy: " << typeid(ActualTypeForProxy).name() << std::endl;
std::cout << " ActualSizeForProxy: " << ActualSizeForProxy << std::endl;
std::cout << " requestSizeP: " << requestSizeP << std::endl;
}
// ... in main:
// Call some_func
// We pass MyConcreteProxy as the template template argument.
// We pass int and 20 as the types to instantiate MyConcreteProxy with.
// We pass 10 as the size for Request.
some_func<MyConcreteProxy, double, 25>(request_obj);
Here, ActualTypeForProxy and ActualSizeForProxy are still separate template arguments to some_func. They are not automatically deduced from TT_Proxy itself.
To achieve something closer to your goal with vvvvWWWW using template metaprogramming:
If vvvvWWWW needs to know AAA and sizeP that are "associated" with some response-like thing, but you don't want to pass response itself, you generally have these options:
-
Explicitly pass AAA and sizeP as template arguments (as you currently do, but with the type trait for correctness):
// Call:
vvvvWWWW<typename extract_inner_type<decltype(response_obj)>::type,
extract_inner_type<decltype(response_obj)>::size>(request_obj);
This is the most common and straightforward C++ way. AAA and sizeP are fully specified.
-
Pass a "tag" type representing Response<AAA, sizeP>:
template<typename T> struct TypeTag {};
template <size_t reqSizeP, typename ResponseType> // Note: AAA is not a template param here
void vvvvWWWW_tagged(const Request<reqSizeP>& request, TypeTag<ResponseType> /*tag*/) {
// Extract AAA and sizeP from ResponseType using your trait
using AAA = typename extract_inner_type<ResponseType>::type;
constexpr size_t respSizeP = extract_inner_type<ResponseType>::size;
// Now you can check consistency or use them
static_assert(reqSizeP == respSizeP, "Request and Response size mismatch!");
std::cout << "vvvvWWWW_tagged called.\n";
std::cout << " AAA is: " << typeid(AAA).name() << std::endl;
std::cout << " reqSizeP (and respSizeP) is: " << reqSizeP << std::endl;
}
// ... in main:
vvvvWWWW_tagged(request_obj, TypeTag<decltype(response_obj)>{});
This passes type information as a zero-overhead argument.
-
If AAA and sizeP could be members of Request<sizeP> itself (less likely given your setup):
If Request was something like Request<UnderlyingType, sizeP>, then vvvvWWWW could deduce UnderlyingType as AAA. This changes your design.
Conclusion on the C++20 question:
The syntax template <typename class<typename AAA, size_t> Proxy, size_t sizeP> is not standard C++ for achieving your goal of deducing AAA and the template sizeP argument of Proxy just by naming Proxy. Template template parameters are about passing template names, and you still need to provide the arguments to instantiate that template (ActualTypeForProxy, ActualSizeForProxy in my example) either explicitly or through deduction from function arguments.
C++20 doesn't introduce a new mechanism to directly "extract" template arguments from a type name in the way your proposed syntax suggests for the purpose of making them available as template parameters to the outer function without an instance. Type traits (like extract_inner_type) remain the standard way to inspect and deconstruct types.
Your note "typename AAA is not ::value_type and I have not code for accessing" is key. You correctly identify that ::value_type gives you Wrapper<...>. To get the inner type, a custom type trait like extract_inner_type is necessary. The methods above show how to use this trait. The most direct way for vvvvWWWW to "know" about AAA and sizeP from a response is if information about response (either the object itself, or its type via a tag or explicit template arguments derived from its type) is passed to vvvvWWWW.