Skip to content

Bugfix/issue 20 allow non copyable non movable return types #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
os:
- Visual Studio 2015
- Visual Studio 2017
- Visual Studio 2017
- Visual Studio 2015

environment:
matrix:
- STD: 17
- STD: 14

build_script:
- git submodule update --init --recursive
- mkdir build
- cd build
- cmake ..
- cmake -DCMAKE_CXX_STANDARD=%STD% -DCMAKE_CXX_STANDARD_REQUIRED=True ..
- cmake --build .
- C:\projects\function-ref\build\Debug\tests.exe

matrix:
exclude:
- os: Visual Studio 2015
STD: 17
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ sudo: false

matrix:
include:
- compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-7
env: COMPILER=g++-7 CXX_STANDARD=17
- compiler: gcc
addons:
apt:
Expand Down
78 changes: 44 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
cmake_minimum_required(VERSION 3.11)

project(tl-function_ref VERSION 1.0.0 LANGUAGES CXX)

option(FUNCTION_REF_ENABLE_TESTS "Enable tests." ON)

include(FetchContent)
cmake_minimum_required(VERSION 3.11)
project(tl-function_ref VERSION 1.0.0 LANGUAGES CXX)
option(FUNCTION_REF_ENABLE_TESTS "Enable tests." ON)
include(FetchContent)
FetchContent_Declare(
tl_cmake
GIT_REPOSITORY https://github.com/TartanLlama/tl-cmake.git
)
GIT_REPOSITORY https://github.com/burnpanck/tl-cmake.git
GIT_TAG b6fe9104d205cf04c4aac3824c2d310959b66120
)
FetchContent_GetProperties(tl_cmake)
if(NOT tl_cmake_POPULATED)
FetchContent_Populate(tl_cmake)
set(CMAKE_MODULE_PATH ${tl_cmake_SOURCE_DIR} ${CMAKE_MODULE_PATH})
endif()
include(add-tl)

tl_add_library(function-ref SOURCES
include/tl/function_ref.hpp)

# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

if(FUNCTION_REF_ENABLE_TESTS)
# Make test executable
set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/call.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp)

add_executable(tests ${TEST_SOURCES})

target_link_libraries(tests Catch function-ref)

set_property(TARGET tests PROPERTY CXX_STANDARD 14)
endif()
endif()
include(add-tl)

tl_add_library(function-ref ARCH_INDEPENDENT SOURCES
include/tl/function_ref.hpp)

# make sure we see the C++ version, even in MSVC:
# https://stackoverflow.com/questions/57102212/cannot-set-cplusplus-to-c17-standard-with-visual-studio-and-cmake
if ((MSVC) AND (MSVC_VERSION GREATER_EQUAL 1914))
target_compile_options(function-ref INTERFACE "/Zc:__cplusplus")
endif()

# Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

if(FUNCTION_REF_ENABLE_TESTS)
# Make test executable
set(TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/call.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp)

add_executable(tests ${TEST_SOURCES})

target_link_libraries(tests Catch function-ref)

message(STATUS "CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
if(NOT CMAKE_CXX_STANDARD)
set_property(TARGET tests PROPERTY CXX_STANDARD 14)
endif()
endif()
10 changes: 9 additions & 1 deletion include/tl/function_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,20 @@ using invoke_result = invoke_result_impl<F, void, Us...>;
template <class F, class... Us>
using invoke_result_t = typename invoke_result<F, Us...>::type;

#if __cplusplus >= 201703
template <typename From, typename To>
using is_prvalue_convertible = std::bool_constant<std::is_same_v<From,To> || std::is_convertible_v<From,To>>;
#else
template <typename From, typename To>
using is_prvalue_convertible = std::is_convertible<From,To>;
#endif

template <class, class R, class F, class... Args>
struct is_invocable_r_impl : std::false_type {};

template <class R, class F, class... Args>
struct is_invocable_r_impl<
typename std::is_convertible<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
typename is_prvalue_convertible<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
: std::true_type {};

template <class R, class F, class... Args>
Expand Down
18 changes: 17 additions & 1 deletion tests/issues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@ TEST_CASE("Issue #10") {
int z = 12;
auto f = [&](const std::vector<int> i) { return i[0] * z; };
foo(f);
}
}

#if __cplusplus >= 201703
struct NonCopyNonMove {
NonCopyNonMove() = default;
NonCopyNonMove(const NonCopyNonMove &) = delete;
NonCopyNonMove(NonCopyNonMove &&) = delete;
};

TEST_CASE("Issue #20") {
auto f = []() { return NonCopyNonMove(); };
auto fr = tl::function_ref<NonCopyNonMove()>(f);

// silence warnings
(void)fr;
}
#endif