Click here to Skip to main content
15,884,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings Kind Regards I am attempting to call a C++ routine which accepts a std::optional<...> argument. However when called w / an argument whose type is the contained type it fails to compile stating the template types could not be deduced. All is shown below i.e.source and output. Kindly advise Thank You Kindly
reference std::optional<T>::optional - cppreference.com[^]
C++
#include <iostream>
#include <optional>

#define LINE_FUNC std::cout << __LINE__ << ' ' << __func__ << std::endl;
#define SHOW(_expression_) std::cout << #_expression_ << ' ' << _expression_ << std::endl;

template<typename T, typename U, U Uvalue>
struct xxx {};

template<typename T, typename U, U Uvalue>
void optional_xxx_arg_function(std::optional<xxx<T, U, Uvalue>>)
{
	LINE_FUNC
}
template<typename T, typename U, U Uvalue>
void xxx_arg_function(xxx<T, U, Uvalue>)
{
	LINE_FUNC
}
// reference https://en.cppreference.com/w/cpp/utility/optional/optional
int main()
{
	std::wcout << L"Hello World\n";
	std::cout << std::boolalpha;

	constexpr int value = 0x159;

	using U = xxx<int, int, value>
	using T = std::optional<U>
	SHOW(typeid(U).name())
	SHOW(typeid(T).name())

	// am not sure these from (8) from reference page are relevant but just in case
	// I do not understand the use of && below as shown on reference page so utilized w/ and w/o &&
	SHOW(std::is_constructible_v<T , U&&>)
	SHOW(std::is_constructible_v<T , U>)
	SHOW(typeid(std::remove_cvref_t<U>).name())
	SHOW(std::is_same_v<std::remove_cvref_t<U> , std::in_place_t>)
	SHOW(std::is_same_v<std::remove_cvref_t<U> , std::optional<T>>)
	SHOW(std::is_convertible_v<U&& , T>)
	SHOW(std::is_convertible_v<U , T>)

	U _xxx_arg;
	T _optional_xxx_arg(_xxx_arg);

	xxx_arg_function(_xxx_arg);
	optional_xxx_arg_function(_optional_xxx_arg);
	xxx_arg_function(*_optional_xxx_arg);
	// I expect below to compile by (8) at reference page but does not
	// optional_xxx_arg_function(_xxx_arg);

	// error message is below
/*
MAIN.CPP(51,36): error C2672: 'optional_xxx_arg_function': no matching overloaded function found
MAIN.CPP(12,6): message : could be 'void optional_xxx_arg_function(std::optional<xxx<T,U,Uvalue>>)'
MAIN.CPP(51,2): message : 'void optional_xxx_arg_function(std::optional<xxx<T,U,Uvalue>>)': could not deduce template argument for 'std::optional<xxx<T,U,Uvalue>>' from 'U'
MAIN.CPP(12): message : see declaration of 'optional_xxx_arg_function'
MAIN.CPP(51,2): message : 'void optional_xxx_arg_function(std::optional<xxx<T,U,Uvalue>>)': could not deduce template argument for 'std::optional<xxx<T,U,Uvalue>>' from 'U'
MAIN.CPP(12): message : see declaration of 'optional_xxx_arg_function'*/
	return 0;
}
Terminal
Hello World
typeid(U).name() struct xxx<int, int, 345>
typeid(T).name() class std::optional<struct xxx<int, int, 345> >
std::is_constructible_v<T , U&&> true
std::is_constructible_v<T , U> true
typeid(std::remove_cvref_t<U>).name() struct xxx<int, int, 345>
std::is_same_v<std::remove_cvref_t<U> , std::in_place_t> false
std::is_same_v<std::remove_cvref_t<U> , std::optional<T>> false
std::is_convertible_v<U&& , T> true
std::is_convertible_v<U , T> true
19 xxx_arg_function
14 optional_xxx_arg_function
19 xxx_arg_function


What I have tried:

Read presumed relevant page at cppreference.com i.e.to wit in particular to be specific namely re / std::optional
Utilized various on - line compilers hoping for more illuminating error messages to no avail
Am currently reading "Template argument deduction" at cppreference.com but my eyes are glazing over
Posted
Updated 31-Aug-22 18:07pm
v2
Comments
Greg Utas 31-Aug-22 10:45am    
I don't think you're notified when a solution is updated, so please see my revised version.

The following compiles for me in C++20 (MSVC). I had to add a few missing semicolons and comment out most of the calls to the SHOW macro, which generated errors for too many arguments. I also added spaces around template arguments to prevent left angle brackets from causing formatting problems. But the std::optional stuff seems to be OK.

EDIT: I uncommented the problem line (third last). I had to supply the template arguments explicitly. I'm not an expert in this, but I don't think the compiler is going to unpack the template arguments in the instantiation of _xxx_arg for you in order to determine what the template arguments for optional_xxx_arg_function are supposed to be.
C++
#include <iostream>
#include <optional>

#define LINE_FUNC std::cout << __LINE__ << ' ' << __func__ << std::endl;
#define SHOW(_expression_) std::cout << #_expression_ << ' ' << _expression_ << std::endl;

template<typename T, typename U, U Uvalue>
struct xxx {};

template<typename T, typename U, U Uvalue>
void optional_xxx_arg_function(std::optional<xxx<T, U, Uvalue>>)
{
	LINE_FUNC
}
template<typename T, typename U, U Uvalue>
void xxx_arg_function(xxx<T, U, Uvalue>)
{
	LINE_FUNC
}
// reference https://en.cppreference.com/w/cpp/utility/optional/optional
int main()
{
	std::wcout << L"Hello World\n";
	std::cout << std::boolalpha;

	constexpr int value = 0x159;

	using U = xxx<int, int, value>;
	using T = std::optional< U >;
	SHOW(typeid(U).name())
	SHOW(typeid(T).name())

	// am not sure these from (8) from reference page are relevant but just in case
	// I do not understand the use of && below as shown on reference page so utilized w/ and w/o &&
// SHOW(std::is_constructible_v<T , U>&&>)
// SHOW(std::is_constructible_v<T , U>)
   SHOW(typeid(std::remove_cvref_t< U >).name())
// SHOW(std::is_same_v<std::remove_cvref_t< U > , std::in_place_t< T >)
// SHOW(std::is_same_v<std::remove_cvref_t< U > , std::optional< T >>)
// SHOW(std::is_convertible_v< U&& , T >)
// SHOW(std::is_convertible_v< U , T >)

	U _xxx_arg;
	T _optional_xxx_arg(_xxx_arg);

	xxx_arg_function(_xxx_arg);
	optional_xxx_arg_function(_optional_xxx_arg);
	xxx_arg_function(*_optional_xxx_arg);
	// I expect below to compile by (8) at reference page but does not
	optional_xxx_arg_function< int, int, value >(_xxx_arg);
	return 0;
}
 
Share this answer
 
v2
Comments
CPallini 31-Aug-22 8:48am    
You left commented the line the OP complained about, namely
  optional_xxx_arg_function(_xxx_arg);
Greg Utas 31-Aug-22 9:16am    
It was commented out in the question.
CPallini 31-Aug-22 9:22am    
Yes, the question is all about that very line (if the OP uncomments it, the compile complains).
Thank You for your kind assistance. Modified code so std::optional is no longer utilized.
 
Share this answer
 
Comments
Greg Utas 1-Sep-22 8:19am    
To reply to a Solution, please use the "Reply" button instead of posting a new "Solution".
BernardIE5317 1-Sep-22 14:37pm    
I see no [Reply] button for "Solution 1" the only solution prior to my own. The only [Reply] button I see is for your Comment but that would not be replying to a solution but to a comment. I only see an [Accept Solution] button which I do not. Further the post to which you refer is meant as a solution. I assume I can post my own solution. Am I mistaken? Further still a window titled "Add your solution here" is below. I assume it means what it says so I can "Add my solution there". Please enlighten me i.e. to wit in particular to be specific namely is an OPs own solution to be posted via a still unseen [Reply] button of an offered but unaccepted solution of which there may be any number or via "Add your solution here"? Thank You Kindly
Greg Utas 1-Sep-22 14:48pm    
My mistake. There is a "Have a Question or Comment?" button for Solution 1 that can be used. The site does not want an OP to post a Solution to their own Question. Just edit the Question instead. Besides, your Solution isn't anywhere near a solution.
BernardIE5317 1-Sep-22 14:59pm    
If I no longer have a question as the result of modifying the code should I perhaps merely delete the question as editing the question w/ my "solution" does not leave any question I need an answer to?
Greg Utas 1-Sep-22 15:07pm    
The moderators don't like questions being deleted. I provided an answer saying that the template arguments had to be provided explicitly, so the question and answer might help someone else with the same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900