c++14

Type conversion from std::complex<MyType> to std::complex<double>

五迷三道 提交于 2020-05-15 08:02:09
问题 I have a class MyType that implements a user-defined arithmetic type. This class provides the following conversion operator struct MyType { ... operator double() { return to_double(); // This converts my type to a double value } ... }; Using this class as follows works fine: double d = MyType(1); However, using this class as type within std::complex, e.g. #include <complex> std::complex<double> c = std::complex<MyType>(1,1); fails with the following compiler error: error: conversion from 'std

Deduce std::array size?

左心房为你撑大大i 提交于 2020-05-13 04:36:14
问题 In the following code: template<size_t N> int b(int q, const std::array<int, N>& types) { int r = q; for (int t : types) { r = r + t; } return r; } int main() { b<2>(9, { 2,3 }); } How can I avoid having to specify 2 in the call to b for N? Why can't this type be automatically deduced? With out it I get the error: 'b': no matching overloaded function found 'int b(int,const std::array &)': could not deduce template argument for 'N' 回答1: C++17 std::array class template argument deduction (CTAD)

Deduce std::array size?

淺唱寂寞╮ 提交于 2020-05-13 04:35:21
问题 In the following code: template<size_t N> int b(int q, const std::array<int, N>& types) { int r = q; for (int t : types) { r = r + t; } return r; } int main() { b<2>(9, { 2,3 }); } How can I avoid having to specify 2 in the call to b for N? Why can't this type be automatically deduced? With out it I get the error: 'b': no matching overloaded function found 'int b(int,const std::array &)': could not deduce template argument for 'N' 回答1: C++17 std::array class template argument deduction (CTAD)

C++ template parameter and partial specialization : strong or weak typing?

白昼怎懂夜的黑 提交于 2020-05-11 06:30:58
问题 Today, a friend of mine and I struggled a lot on a stupid mistake, and I make me wondered about how template parameters work in C++. Consider the following code, where I try to partially specialize a class attr<MyClass<I>> where I is an unsigned int , though MyClass expects an int parameter : #include <iostream> template<int I> class MyClass { }; template<typename T> struct attr; template<unsigned int I> struct attr<MyClass<I>> { }; int main(int argc, char *argv[]) { attr<MyClass<1>> att;

C++ template parameter and partial specialization : strong or weak typing?

岁酱吖の 提交于 2020-05-11 06:30:28
问题 Today, a friend of mine and I struggled a lot on a stupid mistake, and I make me wondered about how template parameters work in C++. Consider the following code, where I try to partially specialize a class attr<MyClass<I>> where I is an unsigned int , though MyClass expects an int parameter : #include <iostream> template<int I> class MyClass { }; template<typename T> struct attr; template<unsigned int I> struct attr<MyClass<I>> { }; int main(int argc, char *argv[]) { attr<MyClass<1>> att;

SFINAE : Delete a function with the same prototype

白昼怎懂夜的黑 提交于 2020-05-07 19:00:25
问题 I wonder what is the difference between this code that works : #include <type_traits> #include <iostream> template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>; template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>; template<typename T, is_ref<T> = true> void foo(T&&) { std::cout << "ref" << std::endl; } template<typename T, is_not_ref<T> = true> void foo(T&&) { std::cout << "not ref" << std::endl; } int main() { int a = 0; foo(a)

May I declare a member type alias to a type in a surrounding scope, using the same name?

江枫思渺然 提交于 2020-04-29 08:17:28
问题 I want a struct to contain a type alias to another type for metaprogramming purposes: struct Foo {}; struct WithNestedTypeAlias { using Foo = Foo; }; Then I can do stuff like WithNestedTypeAlias::Foo in a template etc. As I understand, this type alias is valid because it does not change the meaning of the Foo type. Clang compiles this happily. However, GCC complains: test-shadow-alias.cpp:4:20: error: declaration of ‘using Foo = struct Foo’ [-fpermissive] using Foo = Foo; ^ test-shadow-alias

Can I use a constexpr value in a lambda without capturing it?

半城伤御伤魂 提交于 2020-04-29 07:19:01
问题 I would want to use a constexpr value in a lambda. Reading the answer to Using lambda captured constexpr value as an array dimension , I assumed the following should work: #include<array> int main() { constexpr int i = 0; auto f = []{ std::array<int, i> a; }; return 0; } However, Clang 3.8 (with std=c++14) complains that variable 'i' cannot be implicitly captured in a lambda with no capture-default specified Should this be considered a bug in clang 3.8? BTW: The above code does compile with

Assigning a TopoDS_Face object to its child object compiles with no errors but I have 3 valgrind errors

痴心易碎 提交于 2020-04-18 07:24:04
问题 I have a class called Test which inherits the TopoDS_Face class. Already got some tips from This question but... // Test.h class Test : public TopoDS_Face { public: void operator = (const TopoDS_Face& base_) { TopoDS_Face::operator=(base_); } } // testmain.cpp ... int main() { //extract faces from IGES face for (int i = 1; i <= nbs; i++) { TopoDS_Shape shape = myIgesReader.Shape(i); TopoDS_Face& face = static_cast<TopoDS_Face&>(TopoDS::Face(shape)); Test *test; // tried each of these also and

“a variable declared with an auto specifier cannot appear in its own initializer”

早过忘川 提交于 2020-04-11 12:26:23
问题 It seems like there's an error when using the trailing return type in the function pointer declaration for Func_ptr. I know I can do it if I put the declaration and initialization in the same statement or simply use the standard declaration by specifying the return type directly, but I want to understand the language's limitations, so can someone please explain what this error means in the code below: "a variable declared with an auto type specifier cannot appear in its own initializer"