Here are the function declarations that result in this error
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes) {
write_optimized_model_to_disk(unoptimized_model_path, unoptimized_model_bytes, optimized_model_bytes, NULL);
}
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const fs::path &optimized_path) {
fs::path optimized_model_path = optimized_path;
if(!optimized_model_path) {
optimized_model_path = get_optimized_model_path(unoptimized_model_path);
}
....
The error itself is
/opt/workspace/IhmActon/model_optimizer/src/model_optimizer/model_optimizer.cpp:144:111: error: conversion from ‘long int’ to ‘const boost::filesystem::path’ is ambiguous
write_optimized_model_to_disk(unoptimized_model_path, unoptimized_model_bytes, optimized_model_bytes, NULL);
I want the first function to call the second function with null being set as the last parameter. The second function will then do a null check against that parameter and assign it if it's null
How do I fix this error? I don't understand why I'm getting an is ambiguous error
CodePudding user response:
You are trying to pass NULL
(aka long int
with a value of 0, in your case) to a const fs::path&
parameter. An fs::path
object cannot be constructed from a long int
value.
Since you want your parameter to be "optional", you have a few choices:
- Pass a temporary object to bind the reference to:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes) {
return write_optimized_model_to_disk(unoptimized_model_path, unoptimized_model_bytes, optimized_model_bytes, fs::path() /* or simply: {} */);
}
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const fs::path &optimized_path) {
...
}
Alternatively, use a single function with a default parameter value, instead of two functions:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const fs::path &optimized_path = {}) {
...
}
- Use a pointer instead of a reference:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes) {
return write_optimized_model_to_disk(unoptimized_model_path, unoptimized_model_bytes, optimized_model_bytes, nullptr);
}
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const fs::path *optimized_path) {
fs::path optimized_model_path;
if (optimized_path)
optimized_model_path = *optimized_path;
if (!optimized_model_path) {
optimized_model_path = get_optimized_model_path(unoptimized_model_path);
}
...
}
Alternatively, using a default parameter value:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const fs::path *optimized_path = nullptr) {
...
}
- Use
std::optional
:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes) {
return write_optimized_model_to_disk(unoptimized_model_path, unoptimized_model_bytes, optimized_model_bytes, std::nullopt);
}
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const std::optional<fs::path> &optimized_path) {
fs::path optimized_model_path;
if (optimized_path)
optimized_model_path = *optimized_path.value;
if (!optimized_model_path) {
optimized_model_path = get_optimized_model_path(unoptimized_model_path);
}
}
Alternatively:
fs::path write_optimized_model_to_disk(const fs::path &unoptimized_model_path,
const std::vector<uint8_t> &unoptimized_model_bytes,
const std::vector<uint8_t> &optimized_model_bytes,
const std::optional<fs::path> &optimized_path = std::nullopt) {
...
}
CodePudding user response:
I want the first function to call the second function with null being set as the last parameter. The second function will then do a null check against that parameter and assign it if it's null How do I fix this error? I don't understand why I'm getting an is ambiguous error
When it comes to reference arguments, you are supposed to pass instances of the types the reference points to. NULL
usually is just defined as zero (in case of your compiler it's apparently zero of long int
), and it's incompatible with const boost::filesystem::path
. You may use pointer arguments, which indeed can be implicitly casted from NULL
(you better use nullptr
in this case, though) however in general I would recommend to avoid using raw pointers and prefer smart pointers for scenarios where pointers are required.
Here is how your function may look like (omitting other arguments):
fs::path write_optimized_model_to_disk(...., std::unique_ptr<const fs::path> optimized_path)