Returning an optional from a templated conversion function

I would like to update conversion functions which have the following signatures:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template<typename In, typename Out>
bool Convert(const In& p_in, Out& p_out);
// Returns `true` if the conversion succeeded, `false` otherwise.
</code>
<code>template<typename In, typename Out> bool Convert(const In& p_in, Out& p_out); // Returns `true` if the conversion succeeded, `false` otherwise. </code>
template<typename In, typename Out>
bool Convert(const In& p_in, Out& p_out);

// Returns `true` if the conversion succeeded, `false` otherwise.

so that I could use std::optional instead. Something like (notice template parameters were switched):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template<typename Out, typename In>
std::optional<Out> Convert(const In& p_in);
</code>
<code>template<typename Out, typename In> std::optional<Out> Convert(const In& p_in); </code>
template<typename Out, typename In>
std::optional<Out> Convert(const In& p_in);

There is a big change as far as templates are concerned: One of the templated type is now used in the return type of Convert. Because of this, compilation fails unless I specify function parameters. Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <optional>
class A
{
public:
A() = default;
A(int p_value)
: m_value(p_value)
{}
private:
int m_value = 0;
};
template<typename In, typename Out>
bool Convert(const In& p_in, Out& p_out)
{
return false;
}
template<>
bool Convert(const int& p_in, A& p_out)
{
p_out = A(p_in);
return true;
}
template<typename Out, typename In>
std::optional<Out> Convert2(const In& p_in)
{
return std::nullopt;
}
template<>
std::optional<A> Convert2(const int& p_in)
{
return A(p_in);
}
int main()
{
const int in = 2;
// Fine, but not ideal since we have `std::optional` available:
A out;
const bool result1 = Convert(in, out);
// Failed:
//const std::optional<A> result2 = Convert2(in);
// Ok, but sadly A is repeated at called site:
const std::optional<A> result3 = Convert2<A>(in);
return 0;
}
</code>
<code>#include <optional> class A { public: A() = default; A(int p_value) : m_value(p_value) {} private: int m_value = 0; }; template<typename In, typename Out> bool Convert(const In& p_in, Out& p_out) { return false; } template<> bool Convert(const int& p_in, A& p_out) { p_out = A(p_in); return true; } template<typename Out, typename In> std::optional<Out> Convert2(const In& p_in) { return std::nullopt; } template<> std::optional<A> Convert2(const int& p_in) { return A(p_in); } int main() { const int in = 2; // Fine, but not ideal since we have `std::optional` available: A out; const bool result1 = Convert(in, out); // Failed: //const std::optional<A> result2 = Convert2(in); // Ok, but sadly A is repeated at called site: const std::optional<A> result3 = Convert2<A>(in); return 0; } </code>
#include <optional>

class A
{
public:
    A() = default;

    A(int p_value)
    : m_value(p_value)
    {}

private:
    int m_value = 0;
};

template<typename In, typename Out>
bool Convert(const In& p_in, Out& p_out)
{
    return false;
}

template<>
bool Convert(const int& p_in, A& p_out)
{
    p_out = A(p_in);
    return true;
}

template<typename Out, typename In>
std::optional<Out> Convert2(const In& p_in)
{
    return std::nullopt;
}

template<>
std::optional<A> Convert2(const int& p_in)
{
    return A(p_in);
}

int main()
{
    const int in = 2;

    // Fine, but not ideal since we have `std::optional` available:
    A out;
    const bool result1 = Convert(in, out);

    // Failed:
    //const std::optional<A> result2 = Convert2(in);

    // Ok, but sadly A is repeated at called site:
    const std::optional<A> result3 = Convert2<A>(in);

    return 0;
}

Here is the error message (gcc) if I uncomment the second call:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><source>: In function 'int main()':
<source>:62:46: error: no matching function for call to 'Convert2(const int&)'
62 | const std::optional<A> result2 = Convert2(in);
| ~~~~~~~~^~~~
<source>:30:20: note: candidate: 'template<class Out, class In> std::optional<_Tp> Convert2(const In&)'
30 | std::optional<Out> Convert2(const In& p_in)
| ^~~~~~~~
<source>:30:20: note: template argument deduction/substitution failed:
<source>:62:46: note: couldn't deduce template parameter 'Out'
62 | const std::optional<A> result2 = Convert2(in);
| ~~~~~~~~^~~~
</code>
<code><source>: In function 'int main()': <source>:62:46: error: no matching function for call to 'Convert2(const int&)' 62 | const std::optional<A> result2 = Convert2(in); | ~~~~~~~~^~~~ <source>:30:20: note: candidate: 'template<class Out, class In> std::optional<_Tp> Convert2(const In&)' 30 | std::optional<Out> Convert2(const In& p_in) | ^~~~~~~~ <source>:30:20: note: template argument deduction/substitution failed: <source>:62:46: note: couldn't deduce template parameter 'Out' 62 | const std::optional<A> result2 = Convert2(in); | ~~~~~~~~^~~~ </code>
<source>: In function 'int main()':
<source>:62:46: error: no matching function for call to 'Convert2(const int&)'
   62 |     const std::optional<A> result2 = Convert2(in);
      |                                      ~~~~~~~~^~~~
<source>:30:20: note: candidate: 'template<class Out, class In> std::optional<_Tp> Convert2(const In&)'
   30 | std::optional<Out> Convert2(const In& p_in)
      |                    ^~~~~~~~
<source>:30:20: note:   template argument deduction/substitution failed:
<source>:62:46: note:   couldn't deduce template parameter 'Out'
   62 |     const std::optional<A> result2 = Convert2(in);
      |                                      ~~~~~~~~^~~~

Using auto, I was able to get this to work:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template<typename In>
auto Convert(const In& p_in)
{
return std::nullopt;
}
template<>
auto Convert(const int& p_in)
{
return std::make_optional<A>(p_in);
}
</code>
<code>template<typename In> auto Convert(const In& p_in) { return std::nullopt; } template<> auto Convert(const int& p_in) { return std::make_optional<A>(p_in); } </code>
template<typename In>
auto Convert(const In& p_in)
{
    return std::nullopt;
}

template<>
auto Convert(const int& p_in)
{
    return std::make_optional<A>(p_in);
}

This has the advantage of being usable as I want at call site:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const std::optional<A> result4 = Convert3(in)
</code>
<code>const std::optional<A> result4 = Convert3(in) </code>
const std::optional<A> result4 = Convert3(in)

But I feel the function signature is now very much less informative on what the function does. One has to read the implementation to know a std::optional<A> is returned.

Would there be a way to make this work, without having to specify templated types at call site?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>template<typename Out, typename In>
std::optional<Out> Convert(const In& p_in)
{
// ?
}
template<>
std::optional<A> Convert(const int& p_in)
{
// ?
}
int main()
{
const std::optional<A> result = Convert(in)
return 0;
}
</code>
<code>template<typename Out, typename In> std::optional<Out> Convert(const In& p_in) { // ? } template<> std::optional<A> Convert(const int& p_in) { // ? } int main() { const std::optional<A> result = Convert(in) return 0; } </code>
template<typename Out, typename In>
std::optional<Out> Convert(const In& p_in)
{
    // ?
}

template<>
std::optional<A> Convert(const int& p_in)
{
    // ?
}

int main()
{
    const std::optional<A> result = Convert(in)

    return 0;
}

Sources on Godbolt : https://godbolt.org/z/bYjrcPsPx.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật